简体   繁体   English

如何选择仅在另一列中包含空值的分组行?

[英]How can I select a grouped row where it only contains null values in another column?

It's a bit confusing, so I'll try to exemplify in the table below:这有点令人困惑,所以我将尝试在下表中举例说明:

example-table示例表

id ID data1数据1 data2数据2 data3数据3
1 1 NULL空值 NULL空值 NULL空值
1 1 NULL空值 NULL空值 NULL空值
2 2 1 1 NULL空值 1 1
2 2 1 1 NULL空值 NULL空值
2 2 1 1 NULL空值 1 1
3 3 1 1 NULL空值 NULL空值
3 3 1 1 NULL空值 1 1
4 4 NULL空值 NULL空值 NULL空值

So, grouping by the same ID, I only want to display those IDs that all their data, from all columns, are null.因此,按相同的 ID 分组,我只想显示那些来自所有列的所有数据都为空的 ID。 In the table example above, only IDs 1 and 4.在上面的表格示例中,只有 ID 1 和 4。

The code I'm trying to use - just for reference:我正在尝试使用的代码 - 仅供参考:

select id
from example-table
group by id, data1, data2, data3
having data1 is null and data2 is null and data3 is null; 

Any suggestions?有什么建议?

You can group by id only and set the conditions in the HAVING clause:您可以仅按 id 分组并在HAVING子句中设置条件:

SELECT id
FROM tablename
GROUP BY id
HAVING COALESCE(MAX(data1), MAX(data2), MAX(data3)) IS NULL;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何选择仅包含NULL值的分组行? - How to select grouped rows with only NULL values? 如果另一列具有多个值,如何选择按列分组的值 - How to select values grouped by column if another column has multiple values 我怎样才能 select 所有不与另一行 null 共享列值的行? - How can I select all the rows which do not share a column value with another row which is null? MYSQL仅在该表的列值与另一表的列值匹配的情况下,才如何从该表中选择数据? - MYSQL How do I select data from one table only where column values from that table match the column values of another table? 如何只显示列为NULL的PHP​​表中的值? - How can I show only values in PHP table where column is NULL? MySQL选择行,其中列仅包含一组值 - MySQL Select rows where column contains only values from a set 如何从SQL的表中选择,其中一列中的元素仅包含另一列的特征,而另一列本身又包含两个特征? - How to select from a table in SQL where element in a column contains only one feature of another column which in turn contains two features in itself? 我如何从 MySQL 表中的 select 行按一列分组,另一列具有所需值 - How do I select rows from a MySQL table grouped by one column with required values in another MySQL-如何选择行在数组中的列? - Mysql - How can I select column where row is in array? SQL:根据列值和 select 行值连接 2 个表,其中分组列值为最大值 - SQL: join 2 tables based on column value and select row values where grouped column value is max
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM