我有2张桌子
table users - id, username, ref_id
table ref - id, name
如何获得没有参考的ids? (ref_id == NULL)
示例数据:
table users:
1, admin, 1
2, mike, NULL
table ref:
1, test
2, tester
3, nick
结果:2,3(免费ID)
答案 0 :(得分:2)
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