查询多个LIKE和NOT IN条件

时间:2013-03-07 00:37:01

标签: mysql sql select

我想在多个字段中搜索相同的值。但逃脱最后一次事件的铭文。有可能吗?

我尝试了以下内容,但没有成功。

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

1 个答案:

答案 0 :(得分:5)

您只需要对OR条件进行分组。

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