我有返回所有数据的mysql查询
SELECT state,`County`,`CountyFIPS`,count(`email_address`) as email
FROM `campaign_emails`
Where state='AK'
AND CountyFIPS NOT IN
(select group_concat(`county_fipscode` separator ',')
from order_cart where flyer_id='1' AND user_id='400'
)
GROUP BY `CountyFIPS`
但是当我手动传递相同的查询时,它返回我的确切值
SELECT state,`County`,`CountyFIPS`,count(`email_address`) as email
FROM `campaign_emails`
Where state='AK'
AND CountyFIPS NOT IN (02261,02220,02180,02170,02240,02020,02090,02110)
GROUP BY `CountyFIPS`
请帮助任何人,原因是什么?
最佳答案
IN
接受SELECT查询的结果,但是一个以逗号分隔的值作为硬编码字符串的字符串只会被视为1个值,因此第一个查询中的所有内容都将匹配。
Mysql将第一个查询中的条件视为:
... AND CountyFIPS NOT IN ('1,2,3,4,5') // this is not the array of values you want to use
因此,您可以将代码简化为:
SELECT state,County,CountyFIPS,count(email_address) as email
FROM campaign_emails Where state='AK'
AND CountyFIPS NOT IN (select county_fipscode from order_cart where flyer_id='1' AND user_id='400') GROUP BY CountyFIPS
^^^^^^^^^^^^^^^ here