Mysql count frequency

左心房为你撑大大i 提交于 2019-12-01 19:49:22

You need to group the rows by the common age, then count how many are in each group:

SELECT age, COUNT(*) AS freq FROM ages GROUP BY age

To then convert it into an array, do this in PHP:

$frequencies = array ();
$result = mysql_query('SELECT age, COUNT(*) AS freq FROM table GROUP BY age');
if($result === false) { handle error here... }
while($row = mysql_fetch_row($result)) {
    $frequencies[$row[0]] = $row[1];
}

You now have an associative array called $frequencies with the ages as keys and their frequency as values.

select age, name, count(*) freq from user_age group by age

Sqlfiddle : http://sqlfiddle.com/#!2/266d5/2

select age, count(*) freq from mytable group by age
select age, count(id) as freq from table group by age
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!