0

我正在使用此命令获取数据库中所有用户的年龄:

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

应该如何选择命令呢?

4

2 回答 2

1

尝试这个

 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
于 2013-07-06T10:54:28.420 回答
1

您可以执行以下操作:

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

这将创建一组同龄用户并对其进行计数。

于 2013-07-06T10:55:02.983 回答