I have these tables:
‘Users’
id email name last_access
1 luca@gmail.com Luca Pluto 2012-10-05 17:21:22.0
2 pippo@gmail.com Irene Pippo 2012-10-05 17:22:25.0
‘Nets_permissions’
user_id network_id perm
1 1234 3
1 1235 1
2 1235 3
I’ve written this query:
SELECT u.id, u.name, n.perm
FROM users as u LEFT OUTER JOIN nets_permissions as n
ON u.id = n.user_id
WHERE n.network_id=1234 AND n.perm <> 3
because I want users that already have a permission for network_id (1234) , but also users that don’t have a permission for the network_id. In other words I would have this result query:
2 null null
because the user Luca Pluto with id=1, for the net 1234, have perm=3 so I want left out. Instead, the user Irene Pippo with id=2 doesn’t have any permission on the 1234 net. So it’s row must have net_id and perm set to null.
My query result is empty. I don’t know why. Without the clause n.perm <> 3 seems to work well, but after also the null value are left out, not only the rws with perm=3.
I’ve also tried in this way:
SELECT u.id, u.name, n.perm FROM users as u LEFT OUTER JOIN
(select * from nets_permissions WHERE network_id=1234) as n on u.id = n.user_id
WHERE n.perm <> 3
but it doesn’t work. without the WHERE clause all works. After no. The result query is empty.
How I can resolve this problem? I need that the perm column is a value or null, I can’t remove this column.
The right solution is:
Here Working with NULL Values there is a expanation of the treatment of NULL values in MySQL
Thank you all for your help!