Query with multiple LIKE and NOT IN condition

mysql, select, sql

Solution

You just need to group the `OR` conditions.

SELECT id, login 
FROM   PERSON 
WHERE (login like '%toto%' OR nickname like '%toto%' OR name like '%toto%')
AND id NOT IN (SELECT p.id FROM PERSONNE p, INSCRIPTION i, EVENT e
               WHERE p.id = i.id_person 
               AND i.id_event = e.id
               AND i.id_event = (SELECT MAX(id) FROM EVENT))
GROUP BY login, nom, pseudo

Problem

I want to search the same value but in multiple fields. But escaping inscriptions for the last event. Is it possible? I've tried the following, but without success. ``` SELECT id, login FROM PERSON WHERE login like '%toto%' OR nickname like '%toto%' OR name like '%toto%' AND id NOT IN ( SELECT p.id FROM PERSONNE p, INSCRIPTION i, EVENT e WHERE p.id = i.id_person AND i.id_event = e.id AND i.id_event = ( SELECT MAX(id) FROM EVENT)) GROUP BY login, nom, pseudo ```

Original source