我的数据库中有这些表格:
" USERS"
id email name last_access
1 user1@gmail.com Name1 Surname1 2012-10-05 13:58:19.0
2 user2@gmail.com Name2 Surname2 2012-10-05 13:56:59.0
3 user3@gmail.com Name3 Surname3 2012-10-05 13:57:57.0
" NETS_PERMISSIONS"
user_id network_id perm
1 1234 3
2 1235 3
现在,我有这个问题:
SELECT DISTINCT user.id, user.name, nets.network_id, nets.perm
FROM users as user LEFT OUTER JOIN nets_permissions AS nets
ON user.id = nets.user_id AND nets.perm <> 3 WHERE user.id!=1 AND nets.network_id=1234
结果查询为空,但我希望如此 - 来自&#39; nets_permissions&#39;在遗留的情况下,在查询中使用perm = 3和user_id以及net_id之类的行(在本例中为ex.user_id = 1和network_id = 1234):在这种情况下,查询的左侧部分为空。 - 来自&#39;用户&#39;我想要每一行,但如果它不在&#39; nets_permissions&#39;表,行有network_id和perm。
所以,从这两个表中,在这个例子中,我会得到这个结果:
id name network_id perm
2 Irene Pippo <null> <null>
3 Luca Niccolini <null> <null>
非常感谢大家的帮助。
答案 0 :(得分:4)
这可以为您提供您想要的 - 只有没有烫发3和nework_id 1234的用户
SELECT u.id, u.name, n.network_id, n.perm
FROM users as u
LEFT JOIN nets_permissions as n
ON u.id = n.user_id AND n.perm = 3 AND n.network_id=1234
WHERE n.network_id IS NULL
答案 1 :(得分:1)
我会这样写(这应该有效):
SELECT DISTINCT u.id, u.name, n.network_id, n.perm
FROM users as u
LEFT OUTER JOIN nets_permissions as n ON u.id = n.user_id
WHERE (n.perm is NULL || n.perm <> 3) AND u.id <> 1 AND n.network_id = 1234