我有这样的表:
id column1 column2
1 a 100
2 a NULL
3 b 200
4 c 300
5 c 400
如何在不包括至少有一个NULL
的分组行的情况下选择行。
预期结果:
id column1 column2
3 b 200
4 c 300
5 c 400
ID 1和2不应包括在内,因为它们包含column1 = a
值的NULL
值。
如果一行有重复的条目,即column1 = a
,并且这些行至少包含一个NULL,则不要将其包含在结果集中。
这些sql将对您有所帮助。
select * from test where column1 not in (
SELECT column1 FROM test WHERE column2 IS null GROUP BY column1)
这里是sqlfiddle
谢谢。
您应该创建这样的子查询。
SELECT column1 FROM table WHERE column2 IS null GROUP BY column1
然后,您应该只选择那些在其列1中不包含子选择值的行,如下所示。
SELECT * FROM table WHERE column1 NOT IN (SELECT column1 FROM table WHERE column2 IS null GROUP BY column1)