LeetCode:Database 17.员工奖金

要求:选出所有 bonus < 1000 的员工的 name 及其 bonus。

Employee 表:

+-------+--------+-----------+--------+
| empId |  name  | supervisor| salary |
+-------+--------+-----------+--------+
|   1   | John   |  3        | 1000   |
|   2   | Dan    |  3        | 2000   |
|   3   | Brad   |  null     | 4000   |
|   4   | Thomas |  3        | 4000   |
+-------+--------+-----------+--------+
empId 是这张表单的主关键字

Bonus 表:

+-------+-------+
| empId | bonus |
+-------+-------+
| 2     | 500   |
| 4     | 2000  |
+-------+-------+
empId 是这张表单的主关键字

Result Table:

+-------+-------+
| name  | bonus |
+-------+-------+
| John  | null  |
| Dan   | 500   |
| Brad  | null  |
+-------+-------+

分析:
1.查询结果为两个表中的内容,且显示Employee 表全部员工,因此需要左外来连接
2.通过bonus<1000(仅能筛选非null值)或者bonus is null筛选出最终数据

SQL语句:

SELECT NAME,bonus
FROM(
SELECT a.name,b.bonus
FROM Employee a
LEFT JOIN bonus b 
ON a.empid=b.empid)
c 
WHERE bonus<1000 OR bonus IS NULL;

你可能感兴趣的:(LeetCode,mysql)