简体   繁体   English

根据条件 SQL 排除行

[英]Excluding rows based on criteria SQL

I am looking for some help in figuring out how to remove all rows where column 1 = a if column 2 also = a.我正在寻找一些帮助来弄清楚如何删除第 1 列 = a 如果第 2 列也 = a 的所有行。

It is quite difficult to describe, so I will use an example below.这很难描述,所以我将在下面使用一个例子。

1    2
a    b
a    a
a    null
b    c
b    e
c    c
d    f
d    a
d    e

The table above is what currently exists, I am looking to write a query which gives the following result上表是当前存在的内容,我正在编写一个查询,该查询给出以下结果

1    2
b    c
b    e
d    f
d    a
d    e

If any of them match, ie column 1 = a column 2 = a, then all rows where column 1 = a should be removed.如果它们中的任何一个匹配,即列 1 = 列 2 = a,则应删除列 1 = a 的所有行。 Is there any way to do this?有没有办法做到这一点?

TIA TIA

You seem to want rows where col1 has not rows where col1 = col2 .您似乎想要col1没有col1 = col2行的行。 If that is a correct interpretation, you can use a subquery:如果这是一个正确的解释,您可以使用子查询:

select t.*
from t
where not exists (select 1
                  from t t2
                  where t2.col1 = t.col1 and
                        t2.col1 = t2.col2
                 );

You can try the following query using <> (not) operator.您可以使用<> (not)运算符尝试以下查询。

Select * from Yourtable where [Col1] <> [Col2] and Col1 <> 'a'

Here is the live db<>fiddle demo.这是现场db<>fiddle演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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