简体   繁体   English

MySQL-如何返回不同的ID,其中相同ID的所有行的字段值均为空

[英]MySQL - How to return distinct IDs where all rows for the same ID have null field value

I have a query with two joins that returns this data: 我有一个带有两个联接的查询,该查询返回此数据:

ID Score ID分数
1 NULL 1空
1 5 1 5
1 6 1 6
2 NULL 2空
2 NULL 2空
3 5 3 5
3 8 3 8
3 3 3 3
3 NULL 3空
3 NULL 3空
3 7 3 7
4 NULL 4 NULL
4 NULL 4 NULL
4 3 4 3
4 9 4 9

I would like to return the unique IDs which have a NULL value in the Score column for each of the rows with the same ID. 我想为具有相同ID的每一行返回在Score列中具有NULL值的唯一ID。 In this case, the query should only return one row with the ID of 2 since that is the only ID which has all NULL values in the Score column. 在这种情况下,查询应仅返回ID为2的一行,因为那是在Score列中唯一具有所有NULL值的ID。

Thank you! 谢谢!

You could aggregate your original query by making it as a sub select and aplly count() on score column which has null values, So if all values are NULL for a particular ID then count will return 0, thus using having clause you can filter your results using result of count(Score) 您可以通过将原始查询作为子选择并在具有空值的score列上适当地使用count()来汇总原始查询,因此,如果特定ID所有值均为NULL,则count将返回0,因此可以使用having子句过滤您的使用count(Score)结果的结果count(Score)

select ID,
  count(Score) count_null 
from (your query)t
group by ID
having count_null = 0

Demo Based on your sample data set 演示基于您的样本数据集

暂无
暂无

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

相关问题 MySQL-返回不同的ID,其中相同ID的所有行都没有特定的字段值 - MySQL - Return distinct IDs where all rows for the same ID do not have specific field value 如何计算具有特定值的行(ID),如果MySQL中相同的ID具有其他值,该如何排除呢? - How to count rows (IDs) that have a specific value and exclude if the same ID has other value in MySQL? 如何选择不同的行,其中一列可能具有许多相同的值,但所有第二列均具有相同的值? - How do I select distinct rows where a column may have a number of the same values but all their 2nd columns have the same value? MySQL:返回具有相同ID的所有行,但按不同字段过滤 - MySQL: Return all rows with same ID but filter by a different field 如何使用SQL / mySQL选择2列具有相同值的多个且1列具有不同值的行? - How do I use SQL/mySQL to select rows where 2 columns have multiple of the same value and 1 column has a distinct value? 获取所有行具有相同列值的mysql数据 - Getting mysql data where all rows have same column value mysql从id中选择所有具有相同值的行 - mysql select all rows that have same value from id 如何合并具有相同日期和参与者ID(MYSQL)的所有行 - How to consolidate all rows that have the same date and participant id (MYSQL) 获取其中字段的所有值都具有特定值的ID - Get ids where all values of a field have a certain value 根据不同列的行之一上的值返回具有相同 id 的所有行 - Return all rows that have the same id based on a value on one of the rows of a different column
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM