我有返回所有数据的 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
关于mysql - 如何获取不在下一个表mysql中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39077007/