[英]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.