[英]How to find rows where all values of a column are same?
I have a table user_test_access
which stores test_id
and user_id
.我有一个表user_test_access
存储test_id
和user_id
。
user_test_access
table stores all the uses who have access to the test as well as which user created the test. user_test_access
表存储有权访问测试的所有用户以及创建测试的用户。
id ID | test_creator测试创建者 | test_id测试编号 | user_id用户身份 |
---|---|---|---|
1 1个 | 0 0 | 1 1个 | 901 901 |
2 2个 | 0 0 | 1 1个 | 903 903 |
3 3个 | 0 0 | 2 2个 | 904 904 |
4 4个 | 0 0 | 2 2个 | 905 905 |
5 5个 | 0 0 | 3 3个 | 906 906 |
6 6个 | 1 1个 | 3 3个 | 907 907 |
7 7 | 0 0 | 3 3个 | 908 908 |
I want a query to return all the test_id
where there is no creator.我想要一个查询来返回没有创建者的所有test_id
。 ie test_creator = 0.即 test_creator = 0。
Desired Result:期望的结果:
For the particular data set the answer would be test_id
1 and 2. The reason test_id 3 is not included is because user_id
907 is the test_creator
for it.对于特定的数据集,答案将是test_id
1 和 2。不包括 test_id 3 的原因是因为user_id
907 是它的test_creator
。
What I've tried:我试过的:
SELECT test_id from user_test_access WHERE id = ALL(SELECT id from user_test_access WHERE test_creator=0)
Can you please help me figure out what I'm doing wrong?你能帮我弄清楚我做错了什么吗?
If a missing testcreator is encoded by the value 0
, you can just group by the test_id
and select only ids where the sum is zero如果缺少的 testcreator 由值0
编码,则可以仅按test_id
和 select 仅对总和为零的 ID 进行分组
select test_id
from user_test_table
group by test_id
having sum(test_creator) = 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.