我有这些表:
| NUMBER | NUMBER_LIST |
| 520 | 1 |
| 430 | 1 |
| 870 | 1 |
| 430 | 2 |
我想获得NUMBER_LIST = 1中存在的所有NUMBER个,但我不会得到NUMBER_LIST = 2中存在的NUMBER个。最后我应该得到数字870和520但我不应该得到数字430因为这个数字存在于NUMBER_LIST = 2。
我怎么做?
答案 0 :(得分:2)
这是一种简单的方法:
select NUMBER from TABLE
where NUMBER_LIST = 1
and NUMBER not in (select NUMBER from TABLE where NUMBER_LIST=2)
或者您可以使用自联接而不是子查询:
select NUMBER
from TABLE t1
left join TABLE t2 on t1.NUMBER=t2.NUMBER and t2.NUMBER_LIST=2
where t1.NUMBER_LIST = 1
and t2.NUMBER is null
答案 1 :(得分:0)
试试这个
select number from table1 group by number having count(number) = 1
for MSSQL
结果:
| number |
|--------|
| 520 |
| 870 |