从MySql中选择年龄并将其显示在图表控件中
本文关键字:显示 控件 选择 MySql | 更新日期: 2023-09-27 18:21:21
我使用这个命令来获取数据库中所有用户的年龄:
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
的Chart控件中。
有点像:
Age count
15 30
16 19
18 46
select命令应该如何执行?
尝试这个
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
这将创建一组同龄用户并对其进行计数。