2017-06-01 77 views
0

是否可以在CASE中使用count,這裏是我的查詢。給我一個錯誤#1111使用COUNT的Mysql insde CASE

SELECT t1.id,t1.name,t1.date_entered,t1.date_modified, t1.adresse,t1.adresse_state,t1.adresse_city,t1.adresse_postalcode,t1.hausnummer,t1.center_lat,t1.center_long, t3.name as street_name,t3.strasse,t3.primaerstr 
FROM hi_haus as t1 
LEFT JOIN hi_haus_hi_strasse as t2 ON (t2.hi_haus_id = t1.id) 
LEFT JOIN hi_strasse as t3 ON (t3.id = t2.hi_strasse_id) 
WHERE t1.adresse !='' AND t1.adresse IS NOT NULL AND t1.adresse NOT LIKE '%bau%' AND t1.id IN ('58e722d6-44f5-4e24-8861-5cd4a6f07cbc','4d0a79ee-decf-49b1-8b4b-a912b278ffec') 
AND t3.primaerstr = 
CASE 
WHEN count(t1.id) > '1' THEN t3.primaerstr='0' 
ELSE 
t3.primaerstr = '1' 
END 
GROUP BY t1.id 

我需要做的是讓primaerstr「0」,如果ID被重複多次。

+0

您只能選擇分組的那些列。在這種情況下,你是由t1.id分組,並選擇t1.name,t1.date等 –

+0

我也選擇t1.id,查詢如果我不使用CASE,並具體使用計數的情況下 –

+0

無論內容如何,​​您是基於連接還是正好計算表? – maSTAShuFu

回答

0

SELECT t1.id,t1.name,t1.date_entered,t1.date_modified,t1.adresse,t1.adresse_state,t1.adresse_city,t1.adresse_postalcode,T1。 hausnummer,t1.center_lat,t1.center_long,t3.name as street_name,t3.id,t3.strasse,t3.primaerstr FROM hi_haus as t1 LEFT JOIN hi_haus_hi_strasse as t2 ON(t2.hi_haus_id = t1.id)LEFT JOIN hi_strasse as t3 ON(t3.id = t2.hi_strasse_id)WHERE t1.adresse!=''AND t1.adresse IS NOT NULL AND t1.adresse NOT LIKE'%bau%'AND t1.id IN('58e722d6-44f5-4e24- 8861-5cd4a6f07cbc','5d492f56-c26e-4c03-b3ec-e1d9b0c6f060','fc87b46a-5fe8-4ed0-b3f4-f6fbb0d6919d')AND t3.primaerstr!=(CASE WHEN(SELE CT COUNT(hi_haus_id)FROM hi_haus_hi_strasse WHERE hi_haus_id = t1.id)> 1 THEN 1 ELSE''END)ORDER BY t1.id,t1.date_modified DESC LIMIT 30;

0

試試這個

SELECT t1.id , t1.name , t1.date_entered , t1.date_modified , t1.adresse , t1.adresse_state , t1.adresse_city , t1.adresse_postalcode , t1.hausnummer , t1.center_lat , t1.center_long , t3.name AS street_name , t3.strasse , t3.primaerstr FROM hi_haus AS t1 , (SELECT id , COUNT(*) AS count FROM hi_haus GROUP BY id ) AS subquery LEFT JOIN hi_haus_hi_strasse AS t2 ON (t2.hi_haus_id = t1.id) LEFT JOIN hi_strasse AS t3 ON (t3.id = t2.hi_strasse_id) WHERE t1.adresse != '' AND t1.adresse IS NOT NULL AND t1.adresse NOT LIKE '%bau%' AND t1.id IN ('58e722d6-44f5-4e24-8861-5cd4a6f07cbc', '4d0a79ee-decf-49b1-8b4b-a912b278ffec') AND ((t3.primaerstr = 0 AND t1.id = subquery.id AND subquery.count > 1) OR (t3.primaerstr = 1 AND t1.id = subquery.id AND subquery.count = 0) ) GROUP BY t1.id

+0

感謝您的幫助Abhijith,但仍然錯誤#1054 –

+0

#1054是未知列。檢查是否有任何列拼寫錯誤。對不起,在這裏不能舀湯 –

+0

好吧,我已經檢查過是否有拼寫錯誤。但不幸的是,事實並非如此。我在加入的基礎上計數ID,我認爲這可能是問題。但再次感謝 –