结合力扣:
https://leetcode-cn.com/problemset/all/
1.组合两个表
考点是多表联查格式,以及left join
select Person.FirstName,Person.LastName,Address.City,Address.State from Person left join
Address on Person.PersonID = Address.PersonID
2.第二高的薪水
参考链接: https://www.runoob.com/mysql/mysql-func-ifnull.html
考点ifnull
含义:用于判断第一个表达式是否为 NULL,如果为 NULL 则返回第二个参数的值,如果不为 NULL 则返回第一个参数的值
注意:
在ifnull加as作用是查询的字段值为null的话给他一个不是null的默认值
select ifnull((select distinct Salary from employee order by Salary desc limit 1,1),null) as SecondHighestSalary
结果是:
{"headers": ["SecondHighestSalary"], "values": [[200]]}
select ifnull((select distinct Salary from employee order by Salary desc limit 1,1),null)
结果:
{"headers": ["ifnull((select distinct Salary from employee order by Salary desc limit 1,1),null)"], "values": [[200]]}
181.超过经理收入的员工
参考:https://www.cnblogs.com/zhoug2020/p/4913302.html
考点:
自连接:
as设置别名:表别名只在执行查询时使用,并不在返回结果中显示。而字段定义别名之后,会返回给客户端显示,显示的字段为字段的别名。
两种方式
1.
SELECT
a. NAME AS Employee #给表起别名
FROM
Employee AS a,
Employee AS b
WHERE
a.ManagerId = b.id
AND a.Salary > b.Salary
2.
SELECT
a.NAME AS Employee
FROM Employee AS a JOIN Employee AS b
ON a.ManagerId = b.Id
AND a.Salary > b.Salary
on和where的选择条件的区别:
on后面的是连接条件, 代表两个表建立关系所遵循的规则
where后面的可以看作是筛选条件,是对最终结果集进行过滤所遵循的规则