为什么代码不像内部连接一样行事?在哪里vs和左加入?
编写一个SQL查询,以报告每个员工的名称和奖励金额,奖金少于1000。
Input:
Employee table:
+-------+--------+------------+--------+
| empId | name | supervisor | salary |
+-------+--------+------------+--------+
| 3 | Brad | null | 4000 |
| 1 | John | 3 | 1000 |
| 2 | Dan | 3 | 2000 |
| 4 | Thomas | 3 | 4000 |
+-------+--------+------------+--------+
Bonus table:
+-------+-------+
| empId | bonus |
+-------+-------+
| 2 | 500 |
| 4 | 2000 |
+-------+-------+
Output:
+------+-------+
| name | bonus |
+------+-------+
| Brad | null |
| John | null |
| Dan | 500 |
+------+-------+
SELECT
E.NAME,
(B.BONUS) AS BONUS
FROM EMPLOYEE AS E LEFT JOIN BONUS AS B
ON E.EMPID = B.EMPID WHERE COALESCE(B.BONUS,0)<=1000
我一直在查看一些答案,此代码给出了正确的答案,但是我感到困惑的是,当您使用左表上的子句时,在左联接后,您的查询变成/行为与内部连接一样,但是为什么上面的代码行为不像内部连接,有人可以解释呢?我看过的所有资源都说,左表的过滤器的行为就像内部连接一样,即始终使用和条件,即在这种情况下在e.empid = b.empid and Cocece上(B.Bonus,0) &lt; = 1000
,应该工作吗?谁能解释发生了什么?
Write an SQL query to report the name and bonus amount of each employee with a bonus less than 1000.
Input:
Employee table:
+-------+--------+------------+--------+
| empId | name | supervisor | salary |
+-------+--------+------------+--------+
| 3 | Brad | null | 4000 |
| 1 | John | 3 | 1000 |
| 2 | Dan | 3 | 2000 |
| 4 | Thomas | 3 | 4000 |
+-------+--------+------------+--------+
Bonus table:
+-------+-------+
| empId | bonus |
+-------+-------+
| 2 | 500 |
| 4 | 2000 |
+-------+-------+
Output:
+------+-------+
| name | bonus |
+------+-------+
| Brad | null |
| John | null |
| Dan | 500 |
+------+-------+
SELECT
E.NAME,
(B.BONUS) AS BONUS
FROM EMPLOYEE AS E LEFT JOIN BONUS AS B
ON E.EMPID = B.EMPID WHERE COALESCE(B.BONUS,0)<=1000
I have been looking at some answers and this code gives right answer, but I am confused its said that when you use where clause on left table with left Join, you query becomes/behaves like Inner join, but why isn't the code above behaving like Inner Join, can someone explain? All the resources I have looked at says that Where filter on left table behave like an inner join, i.e. always use AND condition, i.e. in this case ON E.EMPID = B.EMPID AND COALESCE(B.BONUS,0)<=1000
, should work right? Can anyone explain what's happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般而言,关于“查询变为/行为的行为”的短语通常是不正确的。仅当直接在位置中使用右表的列时,这是正确的,并且连接条件定义的右表行的可能性不得存在,因此根据列值可以不考虑列值。您的查询使用Cocece,因此考虑到列值可能是由于连接的行吸收而无效的,并且您的查询会产生所需的输出。
不,它将产生与您需要的输出不同的输出。此类查询变体还将返回
'Thomas'
的行,并在奖励列中返回与所需输出不同的奖金列。如果您将条件从哪里移至输出列表,您可能会了解这一点。比较输出:
并且
还考虑到 - 附加条件
和Cocce(B.Bonus,0)&lt; = 1000
仅应用于现有的奖励表,因此CoceSce用法在案例,并将第三查询添加到比较中,该查询将执行与第二查询一样:PS。您的任务条件告诉:“奖金少于1000”,而不是“少或相等” /“不高于”。因此,您必须不使用
&lt; =
,而是&lt;
。pps。 “但是我感到困惑的是,当您使用左表的左表上的子句时,您会错误地定义左/右表。您必须查看查询文本中的表格 - 因此员工为左表,奖金是右表。当使用左联接时,这意味着“从左表拿起所有行”,右联接表示“从右表中拿起所有行”。
The phrase about "query becomes/behaves like Inner join" is not correct in general. It is correct only when the column from right table is used directly in WHERE, and the possibility that the right table row defined by joining condition must not present and hence the according column value can be NULL is not taken into account. Your query uses COALESCE, so it takes into account that the column value may be NULL due to joined row absense, and your query produces needed output.
No, it will produce the output which differs from one you need. Such query variant will also return the row for
'Thomas'
with NULL in BONUS column which differs from the desired output.You may understand this everything if you'd move the condition from WHERE to the output list. Compare the outputs:
and
Also take into account - additional condition
and COALESCE(B.BONUS,0)<=1000
is applied to existing rows of BONUS table only, so COALESCE usage makes no sense in the case, and add 3rd query to the comparing which will do the same like 2nd query:PS. Your task condition tells: "with a bonus less than 1000", not "less or equal" / "not above". So you must use not
<=
but<
.PPS. "but I am confused its said that when you use where clause on left table with left Join" - you define left/right tables incorrectly. You must look at the table posession in the query text - so EMPLOYEE is left table and BONUS is right table. When LEFT JOIN is used than this means "take all rows from left table", and RIGHT JOIN means "take all rows from right table" accordingly.