数据库
首页 > 数据库> > mysql-如何查找出生日期在1980到1996年之间的用户百分比.

mysql-如何查找出生日期在1980到1996年之间的用户百分比.

作者:互联网

我试图找出出生日期在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

我该怎么做?

先感谢您

解决方法:

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`

标签:mysql-error-1064,mysql
来源: https://codeday.me/bug/20191101/1981351.html