I have a table user_test_access
which stores test_id
and user_id
.
user_test_access
table stores all the uses who have access to the test as well as which user created the test.
id | test_creator | test_id | user_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 |
I want a query to return all the test_id
where there is no creator. i.e 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.
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?
2
Answers
If a missing testcreator is encoded by the value
0
, you can just group by thetest_id
and select only ids where the sum is zeroYou can use not exists operator as the following:
See a demo.