3

我正在尝试查找出生日期在 1980 年到 1996 年之间的用户的百分比。

我找不到怎么做:

SELECT Sum
(WHEN userBirthDate BETWEEN '1980/01/01' AND '1996/12/31'
THEN 1 ELSE 0 END) * 100 / Count(userID) as total
FROM user

mysql 返回的错误是语法:

 ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHEN userBirthDate BETWEEN '1980/01/01' AND '1996/12/31' THEN 1 ELSE 0)' at line 1

我该怎么做?

先感谢您

4

3 回答 3

2
SELECT Sum(case WHEN userBirthDate BETWEEN '1980/01/01' AND '1996/12/31'
                THEN 1 
                ELSE 0 
       END) * 100 / Count(userID) as total
FROM `user`

要不就

SELECT Sum(userBirthDate BETWEEN '1980/01/01' AND '1996/12/31') * 100 / Count(userID) as total
FROM `user`
于 2012-07-03T10:29:21.287 回答
1
SELECT Sum
(IF( userBirthDate BETWEEN '1980/01/01' AND '1996/12/31', 1 , 0 )) * 100 / Count(userID) as total
FROM user
于 2012-07-03T10:30:27.750 回答
1
SELECT Sum
(CASE WHEN userBirthDate BETWEEN '1980/01/01' AND '1996/12/31'
THEN 1 ELSE 0 END) * 100 / Count(userID) as total
FROM user
于 2012-07-03T10:32:30.830 回答