解题报告 - 577. Employee Bonus

Select all employee's name and bonus whose bonus is < 1000.

Table:Employee

+-------+--------+-----------+--------+
| empId |  name  | supervisor| salary |
+-------+--------+-----------+--------+
|   1   | John   |  3        | 1000   |
|   2   | Dan    |  3        | 2000   |
|   3   | Brad   |  null     | 4000   |
|   4   | Thomas |  3        | 4000   |
+-------+--------+-----------+--------+
empId is the primary key column for this table.
Table: Bonus

+-------+-------+
| empId | bonus |
+-------+-------+
| 2     | 500   |
| 4     | 2000  |
+-------+-------+
empId is the primary key column for this table.
Example ouput:

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

注意在on中使用and 和 在join后面使用 where的区别
on中使用and, 则过滤条件会作用于join的过程中,join之后的结果都符合and中的条件
如果把on中的and条件,移动到where, 则join中的结果存在不符合and条件的记录,通过where来过滤.

例如: on的时候,使用and把 bouns > 1000的过滤掉,那么join的时候,bonus表中就没有Thomas的bonus记录, join出来的结果中Thomas的bonus记录为null
如果bonus>1000不放在on里,而是移动到where,则join的结果中Thomas的bonus不为null,然后通过where bonus > 1000 再过滤掉

# Write your MySQL query statement below
select b.name, a.bonus 
from Employee b left join Bonus a
on a.empId = b.empId
where a.bonus < 1000 or a.bonus is null;
posted @ 2019-02-28 21:36  JinleiZhang  阅读(112)  评论(0编辑  收藏  举报