[英]How to find rows where all values of a column are same?
我有一个表user_test_access
存储test_id
和user_id
。
user_test_access
表存储有权访问测试的所有用户以及创建测试的用户。
ID | 测试创建者 | 测试编号 | 用户身份 |
---|---|---|---|
1个 | 0 | 1个 | 901 |
2个 | 0 | 1个 | 903 |
3个 | 0 | 2个 | 904 |
4个 | 0 | 2个 | 905 |
5个 | 0 | 3个 | 906 |
6个 | 1个 | 3个 | 907 |
7 | 0 | 3个 | 908 |
我想要一个查询来返回没有创建者的所有test_id
。 即 test_creator = 0。
期望的结果:
对于特定的数据集,答案将是test_id
1 和 2。不包括 test_id 3 的原因是因为user_id
907 是它的test_creator
。
我试过的:
SELECT test_id from user_test_access WHERE id = ALL(SELECT id from user_test_access WHERE test_creator=0)
你能帮我弄清楚我做错了什么吗?
如果缺少的 testcreator 由值0
编码,则可以仅按test_id
和 select 仅对总和为零的 ID 进行分组
select test_id
from user_test_table
group by test_id
having sum(test_creator) = 0
您可以使用 not exists 运算符,如下所示:
SELECT DISTINCT test_id
FROM user_test_access T
WHERE NOT EXISTS (SELECT 1 FROM user_test_access D
WHERE D.test_id=T.test_id AND D.test_creator=1)
看一个演示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.