I have this query:
SELECT DISTINCT
u.id,
u.name,
n.network_id,
n.perm
FROM users as u RIGHT OUTER JOIN
nets_permissions as n ON u.id = n.user_id AND n.perm<> 3
WHERE u.id!=3
and the result is this:
id name network_id perm
1 Luca Niccolini 9124823324095 1
2 Irene Pippo 9124823324095 0
2 Irene Pippo 1234567812345678124 1
Now I want that the result of the query is:
id name network_id perm
1 Luca Niccolini 9124823324095 1
2 Irene Pippo 9124823324095 0
Infact I want to grant the permission on a net to some users. This users can already have a permission on the net, but also on other nets. So in this case the user Luca has only the permission on the net 9124823324095, but the user Irene has two permissions, on two different nets. I want grant the permissions on the net 9124823324095, so I don’t want the third record.
But I can’t filter with the net id, because i could have a user that have a permission on other nets, but not on my net.
In other words I would that
-for users that have two or more record in the query, I can take only the record of my net
-for users with only a record I can take that record.
It’s difficult to explain. Thank you for your help!
The following code works in most dialects of SQL:
Have you considered the case where there are multiple permissions on networks, but none are for your network? These are excluded by your conditions.