Athar Khan Athar Khan - 7 months ago 5
SQL Question

MySQL: select column1 where column2 contains X but not Y and Z and column1 equal to 1234

SELECT member if level is 2 but not 4 or 5 where member equal to 12345

id | member | level
1 | 1234 | 2
2 | 1234 | 4
3 | 1234 | 5


So based on above table select should not return member because they also have level 4 and 5.

How to write it in MySQL query please?

Answer

Try this:

SELECT member 
FROM mytable
GROUP BY member
HAVING SUM(level = 2) > 0 AND SUM(level IN (4, 5)) = 0

Demo here

Comments