+-
在MYSQL中将NOT IN与GROUP CONCAT一起使用
我在 MySQL查询中使用带有“ NOT IN”语句的“ GROUP_CONCAT”功能.但是由于未知的原因,它不会返回正确的值:

这是我的查询不起作用:

select firstname, lastname
from t_user
where (status_Id NOT IN(Select GROUP_CONCAT(id) from t_status where code = 'ACT'
or code = 'WACT'))

Returns 46 rows

这是我的查询工作:

select firstname, lastname
from t_user
where (status_Id NOT IN(1,4))

Returns 397 rows

GROUP_CONCAT子查询的结果

 (Select GROUP_CONCAT(id) from t_status where code = 'ACT' or code = 'WACT') = 1,4.

看来该查询只照顾GROUP_CONCAT子查询返回的第一项.

所以我不知道发生了什么,为什么在两种情况下我都没有相同的结果.

在此先感谢Gael

最佳答案
在这种情况下,您不需要使用GROUP_CONCAT函数,因为它返回的是字符串值. AND 1,4与1和4有很大的不同.

select  firstname, lastname
from    t_user
where   status_Id NOT IN 
        ( Select id 
          from t_status 
          where code = 'ACT' or code = 'WACT'
        )

解决查询的更好方法是使用LEFT JOIN,

SELECT  a.firstname, a.lastname
FROM    t_user a
        LEFT JOIN t_status b
            ON a.t_status = b.id AND
                b.code IN ('ACT', 'WACT')
WHERE   b.id IS NULL
点击查看更多相关文章

转载注明原文:在MYSQL中将NOT IN与GROUP CONCAT一起使用 - 乐贴网