MYSQL - 获取没有推荐的记录

时间:2014-01-11 15:58:57

标签: php mysql sql

我有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)

1 个答案:

答案 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