LEFT OUTER JOIN 和 WHERE EXISTS。它们等价吗?
LEFT OUTER JOIN and WHERE EXISTS. Are they equivalent?
我想使用 LEFT OUTER JOIN 到 WHERE EXISTS 创建等效的 MySQL 查询。我正在关注这个问题:
Are the SQL concepts LEFT OUTER JOIN and WHERE NOT EXISTS basically the same?
这是原始查询:
SELECT *
FROM tableA
JOIN tableB ON tableA.tableA_id = tableB.tableB_id
JOIN tableC ON tableC.tableC_id = tableB.tableB_id
WHERE NOT EXISTS (
SELECT 1
FROM tableD
WHERE tableA.employee_id = tableD.employee_id AND tableC.tableC_datum = DATE(tableD.tableD_od_datetime)
)
但是这个查询 return 不同的值:
SELECT *
FROM tableA
JOIN tableB ON tableA.tableA_id = tableB.tableB_id
JOIN tableC ON tableC.tableC_id = tableB.tableB_id
LEFT OUTER JOIN tableD ON tableA.employee_id = tableD.employee_id AND tableC.tableC_datum = DATE(tableD.tableD_od_datetime)
WHERE tableD.employee_id IS NULL AND DATE(tableD.tableD_od_datetime) IS NULL
请问为什么这两个输出不相等?
not exists
和 left join ... rgt.col is null
方法是相同的。然而,left join
将包含来自不需要的 table 的列,因此只需具体说明 select
子句:
SELECT table_a.*, table_b.*, table_c.*
FROM table_a
JOIN table_b ...
JOIN table_c ...
LEFT JOIN table_d ...
我宁愿完全避免 *
并明确列出我需要的那些列。
我想使用 LEFT OUTER JOIN 到 WHERE EXISTS 创建等效的 MySQL 查询。我正在关注这个问题:
Are the SQL concepts LEFT OUTER JOIN and WHERE NOT EXISTS basically the same?
这是原始查询:
SELECT *
FROM tableA
JOIN tableB ON tableA.tableA_id = tableB.tableB_id
JOIN tableC ON tableC.tableC_id = tableB.tableB_id
WHERE NOT EXISTS (
SELECT 1
FROM tableD
WHERE tableA.employee_id = tableD.employee_id AND tableC.tableC_datum = DATE(tableD.tableD_od_datetime)
)
但是这个查询 return 不同的值:
SELECT *
FROM tableA
JOIN tableB ON tableA.tableA_id = tableB.tableB_id
JOIN tableC ON tableC.tableC_id = tableB.tableB_id
LEFT OUTER JOIN tableD ON tableA.employee_id = tableD.employee_id AND tableC.tableC_datum = DATE(tableD.tableD_od_datetime)
WHERE tableD.employee_id IS NULL AND DATE(tableD.tableD_od_datetime) IS NULL
请问为什么这两个输出不相等?
not exists
和 left join ... rgt.col is null
方法是相同的。然而,left join
将包含来自不需要的 table 的列,因此只需具体说明 select
子句:
SELECT table_a.*, table_b.*, table_c.*
FROM table_a
JOIN table_b ...
JOIN table_c ...
LEFT JOIN table_d ...
我宁愿完全避免 *
并明确列出我需要的那些列。