我想使用GROUP BY子句从表中删除重复项,但我想只在两个字段不为空时才执行此操作。如果任何字段为null,则group by不应删除此记录。
示例:
| ID | IDEK |
| 1 | null |
|null| 1 |
|null| 1 |
|null| null |
|665 | 22 |
|665 | 22 |
现在,当我使用此查询时:
select * from test group by id, idek
我想得到这个结果:
| ID | IDEK |
| 1 | null | // HERE IS NULL SO IT SHOULD BE PRINTED ALWAYS
|null| 1 | // HERE IS NULL SO IT SHOULD BE PRINTED ALWAYS
|null| 1 | // HERE IS NULL SO IT SHOULD BE PRINTED ALWAYS
|null| null | // HERE IS NULL SO IT SHOULD BE PRINTED ALWAYS
|665 | 22 |
有可能吗?以下是现场工作的实例:http://www.sqlfiddle.com/#!9/070218/1
感谢。
答案 0 :(得分:2)
以下是您问题的解决方案:
SELECT id,idek
FROM test
WHERE id IS NULL OR idek IS NULL
UNION ALL
SELECT id,idek
FROM test
WHERE id IS NOT NULL AND idek IS NOT NULL
GROUP BY id,idek
演示链接:
答案 1 :(得分:0)
您可以执行以下操作:
select * from test
where id IS NULL or idek IS NULL
group by id, idek