如何在下一个tabel mysql中获取数据

时间:2016-08-22 10:20:48

标签: mysql sql

我有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`

请帮助任何人,这是什么原因?

1 个答案:

答案 0 :(得分:3)

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