我正在尝试运行如下所示的代码。此中的子查询与主查询来自同一个表 - 唯一的约束是 column3 is/is not NULL。 有什么办法可以简化吗?
SELECT a.column1, a.column2, b.column1, b.column2
FROM table1 a
INNER JOIN
(SELECT column1, column2
FROM table1
WHERE column3 IS NULL) b
ON a.column4 = b.column4
WHERE a.column3 IS NOT NULL
GROUP BY a.column1, a.column2, b.column1, b.column2
最佳答案
编辑后:(在您的查询 b 表中没有 column4)
试试这个:
SELECT a.column1, a.column2, b.column1, b.column2
FROM table1 a
INNER JOIN table1 b
ON a.column4 = b.column4
WHERE a.column3 IS NULL
AND b.column3 IS NOT NULL
如果您认为四列之间存在重复,您可以在字段列表中使用 GROUP BY 或 DISTINCT 命令。
关于mysql - 简化 SQL 查询而不是使用连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19170943/