Mysql任何不等于子查询不起作用

时间:2013-05-12 12:35:15

标签: mysql subquery

我在使用ANY查询时遇到了一个小问题。

Select *, count(*) as m 
from mp_bigrams_raw 
where date_parsed=051213 
and art_source='f' 
and bigram != ANY(select feed_source from mp_feed_sources) 
group by bigram 
order by m DESC 
limit 50;

查询运行但不排除子查询中找到的项目。

subquery中只有一行时,原始查询有效。一旦我添加更多,我得到一个超过1行的错误。

Select *, count(*) as m 
from mp_bigrams_raw 
where date_parsed=051213 
and art_source='f' 
and bigram != (select feed_source from mp_feed_sources) 
group by bigram 
order by m DESC 
limit 50;

从那里我添加了ANY并且查询运行但似乎忽略了!=。我猜我在这里错过了一些东西。

由于

3 个答案:

答案 0 :(得分:2)

为什么不使用 NOT IN

Select *, count(*) as m 
from mp_bigrams_raw 
where date_parsed=051213 
and art_source='f' 
and bigram NOT IN(select feed_source from mp_feed_sources) 
group by bigram 
order by m DESC 
limit 50;

答案 1 :(得分:0)

尝试将left joinis null

一起使用
Select r.*, count(*) as m 
from mp_bigrams_raw r
left join mp_feed_sources f on f.feed_source = r.bigram 
where r.date_parsed=051213 
  and r.art_source='f' 
  and f.feed_source is null
group by r.bigram 
order by m DESC 
limit 50;

答案 2 :(得分:0)

条件 ANY 会返回true(据文档所述),只要条件true subselect 的条目,因此,如果其中一个选定字段为 != bigram ,则该条款的评估结果为 true

NOT IN 是您想要的,因此bigram不在所选值列表中。