如何过滤多列

时间:2014-02-24 09:05:02

标签: sql-server

我的表格中有2列。 C1和C2。我想从

提供的表格中选择数据
C1 not 3 and C2 not 5  
OR   
C1 not 5 nad C2 not 3

以下是我的sql语句

select * from table A
where ((C1 <> 3 and C2 <> 5) AND (C1 <> 5 and C2 <> 3))

以上成功筛选出大多数情况。但它未能选择一些数据,例如 C1 = 5且C2 = 7
我附上了一个打印屏幕,以便更好地阐述。enter image description here

4 个答案:

答案 0 :(得分:1)

您到处都有and,这意味着您的查询 select * from table A where ((C1 <> 3 and C2 <> 5) AND (C1 <> 5 and C2 <> 3))

相同

select * from table A where C1 <> 3 and C2 <> 5 AND C1 <> 5 and C2 <> 3

所以,正确的将是

select * from table A where not (C1 = 3 and C2 = 5) AND not (C1 = 5 and C2 = 3)

答案 1 :(得分:0)

尝试在两个 OR 条件之间使用 AND

select * from table A
where C1 <> 3 AND C2 <> 5 AND C1 <> 5 AND C2 <> 3

答案 2 :(得分:0)

我会选择CASE

 select * from tableA
where (case when C1=3 OR C2=5 then 0
            when C1=5 OR C2=3 then 0
            else 1
      end)=1      

答案 3 :(得分:0)

会起作用吗,

select * from table A
where C1 not in(3,5) and C2  not in(3,5)