公司網(wǎng)站制作 步驟seo優(yōu)化師是什么
數(shù)據(jù)庫操作
- 1、 表之間連接
MYSQL 題- 1、取第二高薪
- 2、取第N高薪
- 3、分?jǐn)?shù)排名
inner join:2表值都存在
outer join:附表中值可能存在null的情況。
總結(jié):
①A inner join B:取交集
②A left join B:取A全部,B沒有對應(yīng)的值,則為null
③A right join B:取B全部,A沒有對應(yīng)的值,則為null
④A full outer join B:取并集,彼此沒有對應(yīng)的值為null
1、取第二高薪
編寫一個(gè)SQL查詢以獲得Employee表中第二高的薪水。
-
---- + -------- +
| Id | 薪水| -
---- + -------- +
| 1 | 100 |
| 2 | 200 |
| 3 | 300 | -
---- + -------- +
例如,給定上面的Employee表,查詢應(yīng)該返回200為第二高薪水。如果沒有第二高的薪水,那么查詢應(yīng)該返回null。 -
--------------------- +
| SecondHighestSalary | -
--------------------- +
| 200 | -
--------------------- +
或者 -
--------------------- +
| SecondHighestSalary | -
--------------------- +
null -
--------------------- +
關(guān)鍵字解釋:
1、子查詢,第一個(gè)select 如果查詢沒有值 為null。沒有第一個(gè)select不能保證沒有第二高薪的為null。
2、distinct 去重,如果一共兩條數(shù)據(jù),salary都是100,則第二個(gè)并不是第二高薪,因?yàn)椴樵兘Y(jié)果并不能并列,所以將薪水為100,進(jìn)行去重(薪水為100 保存一個(gè),這樣就起到一個(gè)并列的效果),這樣第二高薪就為null,結(jié)果合理。
3、
① selete * from testtable limit 2,1;
② selete * from testtable limit 2 offset 1;
注意:
1.數(shù)據(jù)庫數(shù)據(jù)計(jì)算是從0開始的
2.offset X是跳過X個(gè)數(shù)據(jù),limit Y是選取Y個(gè)數(shù)據(jù)
3.limit X,Y 中X表示跳過X個(gè)數(shù)據(jù),讀取Y個(gè)數(shù)據(jù)
這兩個(gè)都是能完成需要,但是他們之間是有區(qū)別的:
①是從數(shù)據(jù)庫中第三條開始查詢,取一條數(shù)據(jù),即第三條數(shù)據(jù)讀取,一二條跳過
②是從數(shù)據(jù)庫中的第二條數(shù)據(jù)開始查詢兩條數(shù)據(jù),即第二條和第三條。
select(select distinct salary
from Employee as em
order by em.salary asc
limit 1 offset 1) as SecondHighestSalary
2、第N高薪水
表: Employee
±------------±-----+
| Column Name | Type |
±------------±-----+
| id | int |
| salary | int |
±------------±-----+
在 SQL 中,id 是該表的主鍵。
該表的每一行都包含有關(guān)員工工資的信息。
查詢 Employee 表中第 n 高的工資。如果沒有第 n 個(gè)最高工資,查詢結(jié)果應(yīng)該為 null 。
示例 1:
輸入:
Employee table:
±—±-------+
| id | salary |
±—±-------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
±—±-------+
n = 2
輸出:
±-----------------------+
| getNthHighestSalary(2) |
±-----------------------+
| 200 |
±-----------------------+
示例 2:
輸入:
Employee 表:
±—±-------+
| id | salary |
±—±-------+
| 1 | 100 |
±—±-------+
n = 2
輸出:
±-----------------------+
| getNthHighestSalary(2) |
±-----------------------+
| null |
±-----------------------+
關(guān)鍵字聲明
1、limit 和offset 上一個(gè)題中講過。
2、
//聲明參數(shù),將形參賦值給你定義的屬性,因?yàn)閿?shù)據(jù)庫下標(biāo)是從0開始的。所以-1操作。
DECLARE A INT;SET A=N-1;
3、整體代碼
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
DECLARE A INT;SET A=N-1;RETURN (# Write your MySQL query statement below.select (select distinct salaryfrom Employeeorder by salary desclimit 1 offset A ) );
END
178. 分?jǐn)?shù)排名
Scores
±------------±--------+
| Column Name | Type |
±------------±--------+
| id | int |
| score | decimal |
±------------±--------+
實(shí)例:
輸入:
Scores 表:
±—±------+
| id | score |
±—±------+
| 1 | 3.50 |
| 2 | 3.65 |
| 3 | 4.00 |
| 4 | 3.85 |
| 5 | 4.00 |
| 6 | 3.65 |
±—±------+
輸出:
±------±-----+
| score | rank |
±------±-----+
| 4.00 | 1 |
| 4.00 | 1 |
| 3.85 | 2 |
| 3.65 | 3 |
| 3.65 | 3 |
| 3.50 | 4 |
±------±-----+
關(guān)鍵字:
mysql中排名函數(shù)有三個(gè):
1、row_number():表示排序,成績相同,也不重復(fù)
2、rank():表示排序,成績相同,排名重復(fù),但跳躍
3、dense_rank():表示排序,排名重復(fù),但不跳躍
解釋:
1、表示排序,成績相同,也不重復(fù)(排名1,2)不會(huì)出現(xiàn)并列。
2、rank():表示排序,成績相同,排名重復(fù),但跳躍
3、dense_rank():表示排序,排名重復(fù),但不跳躍
select score ,DENSE_RANK() OVER(ORDER BY score DESC) as 'rank'
from Scores
order by score DESC