Reputation: 43
I have 2 tables
table users - id, username, ref_id
table ref - id, name
How to get ids who doesn't have a ref? (ref_id == NULL)
example data:
table users:
1, admin, 1
2, mike, NULL
table ref:
1, test
2, tester
3, nick
RESULT: 2,3 (free IDs)
Upvotes: 1
Views: 36
Reputation: 12111
SELECT r.id AS id
FROM users AS u
RIGHT JOIN ref as r
ON ( u.ref_id = r.id )
WHERE u.ref_id IS NULL
Upvotes: 2