我有一张数据表:
id a_no accountname
1 NULL ac1
1 234 ac2
1 567 ac3
2 NULL ac4
我想用以下标准选择行:
a_no=NULL
这样生成的行应该是
id a_no accountname
1 234 ac2
1 567 ac3
2 NULL ac4
如何编写T-SQL查询?感谢。
答案 0 :(得分:1)
select * from yourtable
where a_no is not null
union all
select * from yourtable
where id not in
(select id from your table where a_no is not null)
答案 1 :(得分:0)
DELETE FROM yourtable
WHERE id IN (SELECT id FROM yourtable
WHERE NOT a_no IS NULL)
AND a_no IS NULL
编辑:更简单 编辑II:误读您的问题,您只想选择行,而不是从表中删除它们。如果是这样,您可以使用以下内容:
SELECT * FROM yourtable
WHERE a_no IS NOT NULL
OR id NOT IN (SELECT id FROM yourtable
WHERE a_no IS NOT NULL)
答案 2 :(得分:0)
基于Martinjn查询,我去了:
select * from t
where id not in (
select id from t
where a_no is not null)
or a_no is not null