如何在此MySQL查询中使用CASE?

时间:2011-05-24 21:40:10

标签: mysql

SELECT
DATE(DTM) AS'Dia',
ROUND(COUNT(TMP)/4) AS 'Oídio'
CASE 
ROUND(COUNT(TMP)/4)>=6 THEN '20'
ROUND(COUNT(TMP)/4)<6 THEN '-10'
END
FROM dados_meteo
WHERE TMP>=20 AND TMP<=30
GROUP BY DATE(DTM)

1 个答案:

答案 0 :(得分:4)

你也许正在寻找这个吗?

SELECT
DATE(DTM) AS 'Dia',
ROUND(COUNT(TMP)/4) AS 'Oídio',
CASE 
WHEN ROUND(COUNT(TMP)/4)>=6 THEN '20'
WHEN ROUND(COUNT(TMP)/4)<6 THEN '-10'
END
FROM dados_meteo
WHERE TMP>=20 AND TMP<=30
GROUP BY DATE(DTM)

您需要使用WHEN标记CASE的分支,并且您在CASE之前缺少逗号。