我正在使用此命令获取数据库中所有用户的年龄:
SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(STR_TO_DATE(birthday, '%d/%m/%Y'))), '%Y')+0 AS age
FROM users ;
我想获取每个年龄的计数并将其显示在图表控件中asp.net
。
类似的东西:
Age count
15 30
16 19
18 46
应该如何选择命令呢?
尝试这个
SELECT age , count(age) FROM (
SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(STR_TO_DATE(birthday, '%d/%m/%Y'))), '%Y')+0 AS age FROM users ) t GROUP BY age
您可以执行以下操作:
SELECT AGE , COUNT(*) as count
(
SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())- TO_DAYS(STR_TO_DATE(birthday, '%d/%m/%Y'))), '%Y')+0 AS age
FROM users
) AS A
GROUP BY AGE
这将创建一组同龄用户并对其进行计数。