I have a table with application name and permissions given to the application. i need to find what are the applications that require both these
name permissions
app1 perm1
app1 perm5
app1 perm6
app2 perm1
app2 perm8
app3 perm1
app3 perm6
app3 perm2
app3 perm4
How do i find the application names which has “perm1 and perm6” both….?
Here’s one way to do it.
We’re using a GROUP BY and aggregate functions, if we find a row for that has ‘perm1′, then t.permissons=’perm1’ returns a 1, otherwise it returns a 0. (We handle the case of a NULL by wrapping t.permissions in an IFNULL function.)
If we find both a ‘perm1’ and a ‘perm6’, then the HAVING predicate will evalulate to TRUE, and the row will be returned.
Another way to do it, using an EXISTS predicate to run a correlated subquery:
or an equivalent, using an IN predicate:
A fourth way to get it would be: