Janusz Janusz
Janusz Janusz

Reputation: 43

MYSQL - Get records who doesn't have referral

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

Answers (1)

voodoo417
voodoo417

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

Related Questions