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