Mysql使用COUNT insde CASE

时间:2017-06-01 09:13:36

标签: mysql

是否可以在CASE中使用count,这是我的查询。给了我一个错误#1111

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

如果id重复多次,我需要的是获得primaerstr'0'。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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且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(SELECT COUNT(hi_haus_id)FROM hi_haus_hi_strasse WHERE hi_haus_id = t1.id)> 1那么1 ELSE' END)ORDER BY t1 .id,t1.date_modified DESC LIMIT 30;