[英]Can someone explain to me how this statement is an exclude?
有人可以告诉我这是如何排除的吗?
假设tableID是自动生成的,并且columnY中的值可以为0或1。该语句应排除columnY值为1的所有内容。
SELECT *
FROM [table].[dbo].[one] AS t1
LEFT JOIN [table].[dbo].[one] AS t2
ON (t1.ColumnX = t2.ColumnX AND columnY = 1)
WHERE t1.tableID IS NULL
因此表看起来像这样:
ID | ColumnX | ColumnY
1 Blue 0
2 Blue 1
3 Red 0
4 Red 0
5 Red 1
我可以解释一下...与您的查询非常接近的查询。 让我将其更改为:
SELECT *
FROM [table].[dbo].[one] AS t1
LEFT JOIN [table].[dbo].[one] AS t2
ON (t1.ColumnX = t2.ColumnX AND t2.columnY = 1)
WHERE t2.tableID IS NULL
该查询从t1
检索所有行,然后检查t2
是否存在具有相同ColumnX
值(即ColumnY
为1)的行(即同一表)。
如果该行不存在,则它将使用ColumnX
的值列出t1
中的所有行(以及t2
的一堆空值)。 但是,如果该行确实存在,则将排除具有ColumnX
值的行(因为t2.id IS NOT NULL
)。
这是一个SQLFiddle。
用这个
SELECT *
FROM [table].[dbo].[one] AS t1
LEFT JOIN [table].[dbo].[one] AS t2
ON (t1.ColumnX = t2.ColumnX) AND (columnY = 1)
WHERE t1.tableID IS NULL
因此表看起来像这样:
ID | ColumnX | ColumnY
2 Blue 1
5 Red 1
或者你可以用这个
SELECT *
FROM [table].[dbo].[one] AS t1
LEFT JOIN [table].[dbo].[one] AS t2
ON (t1.ColumnX = t2.ColumnX)
WHERE t2.columnY = 1 AND t1.tableID IS NULL
因此表看起来像这样:
ID | ColumnX | ColumnY
2 Blue 1
5 Red 1
我希望这能帮到您。 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.