我有两张桌子如下。
user_id | username | first_name | role_type
-----------------------------------------------------------
1 | testuser1 | testu1 | student
2 | testuser2 | testu2 | student
3 | testuser3 | testu3 | student
4 | testuser4 | testu4 | student
5 | testuser5 | testu5 | student
6 | testuser6 | testu6 | admin
7 | testuser7 | testu7 | admin
-----------------------------------------------------------
user_id | username | approved_id
----------------------------------------------------------------------
1 | testuser1 | 3B888F52-50BC-11E2-B08B-99E5B2CADDF7
2 | testuser2 | 3B888F52-50BC-11E2-B08B-99E5B2CADDF7
3 | testuser3 | 3B888F52-50BC-11E2-B08B-99E5B2CADDF7
----------------------------------------------------------------------
我试过这个问题
SELECT users.* FROM users
WHERE users.username NOT IN(
SELECT users_approval.username FROM users_approval
WHERE users_approval.approved_id = "3B888F52-50BC-11E2-B08B-99E5B2CADDF7"
) AND users.role_type = "student"
结果如下
user_id | username | first_name | role_type
-------------------------------------------------------------
4 | testuser4 | testu4 | student
5 | testuser5 | testu5 | student
-------------------------------------------------------------
是否有任何方法可以使用JOIN获取与上面相同的结果集?.
帮助是非常有用的。
最佳答案
SELECT users.*
FROM users
LEFT JOIN users_approval b
ON users.username = b.username AND
b.approved_id = "3B888F52-50BC-11E2-B08B-99E5B2CADDF7"
WHERE users.role_type = "student" AND
b.approved_id IS NULL
SQLFiddle Demo
关于mysql - MySQL查询联接以从两个表中选择不匹配的行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14275259/