2013-05-12 31 views
1

我遇到一個使用ANY查詢的小問題。Mysql任何不等於子查詢不起作用

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; 

從那裏我添加了任何和查詢運行,但似乎忽略!=。我猜我在這裏錯過了一些東西。

感謝

+0

什麼數據類型是'date_parsed'列?條件'date_parsed = 05121'看起來很奇怪。如果這是一個日期條件是不正確的,如果它是一個數字的前導零不相關,並且如果它是一個字符列,那麼你需要把它括在單引號。 – 2013-05-12 12:40:09

+0

@a_horse_with_no_name date_parsed無關緊要。 (這是int btw)。 – TheEditor 2013-05-12 12:43:08

回答

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; 
+0

非常感謝。這工作完美。出於好奇,爲什麼!=不工作? – TheEditor 2013-05-12 12:46:39

+0

@TheEditor **'ANY' **返回'true'(據文檔說)。 如果**條件**對於**'subselect ** **的任何條目是「真」,那麼如果任何這些選定的字段是**'!= bigram' **,則該條款評估爲* *'TRUE' **。 **'NOT IN' **是你想要的,所以bigram不在選定的值列表中。你的答案之一。如果它對你有幫助,請嘗試將其作爲你的答案。 – Luv 2013-05-12 12:52:35

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; 
0

條件ANY回報true(只要DOC說),每當條件true爲任何的subselect條目的,因此,如果那些選定字段之一是!= bigram該條的計算結果爲true

NOT IN是你想要的,所以bigram不在選定的值列表中。