[英]Using NOT IN with GROUP CONCAT in MYSQL
I'am using the " GROUP_CONCAT " function with the " NOT IN " statement in my mysql query.我在我的 mysql 查询中使用“ GROUP_CONCAT ”函数和“ NOT IN ”语句。 But for unknown reason, it doesn't return the correct values:但由于未知原因,它没有返回正确的值:
Here is my query not working:这是我的查询不起作用:
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
Here is my query working:这是我的查询工作:
select firstname, lastname
from t_user
where (status_Id NOT IN(1,4))
Returns 397 rows
The results of the of the GROUP_CONCAT subquery GROUP_CONCAT子查询的结果
(Select GROUP_CONCAT(id) from t_status where code = 'ACT' or code = 'WACT') = 1,4.
It seems that the query take only care of the first item return by the GROUP_CONCAT subquery.似乎查询只处理 GROUP_CONCAT 子查询返回的第一项。
So I don't understand what's going on and why I do not have the same results in both cases.所以我不明白发生了什么以及为什么我在两种情况下都没有相同的结果。
Thanks in advance Gael提前致谢 盖尔
in this case, you don't need to use GROUP_CONCAT
function because it returns a string value.在这种情况下,您不需要使用GROUP_CONCAT
函数,因为它返回一个字符串值。 AND和1, 4
is very different from 1
and 4
. 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'
)
and a better way to right the query is by using LEFT JOIN
, LEFT JOIN
查询的更好方法是使用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
It is possible to exploit the list generated from GROUP_CONCAT as your question asks.当您的问题提出时,可以利用从 GROUP_CONCAT 生成的列表。 Thus, in general, you can use the list from GROUP_CONCAT as follows:因此,通常,您可以使用 GROUP_CONCAT 中的列表,如下所示:
According to the specification , you can simply use FIND_IN_SET instead of NOT IN to filter a field by subquery根据规范,您可以简单地使用 FIND_IN_SET 而不是 NOT IN 通过子查询过滤字段
select firstname, lastname
from t_user
where NOT FIND_IN_SET(status_Id,
(Select GROUP_CONCAT(id) from t_status where code = 'ACT' or code = 'WACT')
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.