繁体   English   中英

有人可以向我解释这个陈述是如何排除的吗?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM