MySQL的计数频率 [英] Mysql count frequency
问题描述
我检查了类似的问题,但它在我的precise问题没有帮助。
I've checked similar questions but it didnt help in my precise question.
所以,我的表是这样的:
So, my table goes like this:
id age
1 30
2 36
3 30
4 52
5 52
6 30
7 36
等
我需要计算年龄的频率:
I need to count the frequency of ages:
age freq
30 2
36 3
52 2
我怎么能抓住这个频率?
在此之后,我将需要该数据的工作,所以它可能使用数组是必要的?
谢谢!
How can I grab this freq? After this I will need to work with that data, so it might be necessary using array? Thanks!
function drawChart() {
// Create the data table.
var data = new google.visualization.DataTable();
data.addColumn('string', 'age');
data.addColumn('number', 'freq');
<?php
while($row = mysql_fetch_row($result))
{
$frequencies[$row[0]] = $frequencies[1];
echo "data.addRow(['{$row[0]}', {$row[1]}]);";
}
?>
我们的目标是建立一个图
The goal is build a chart
推荐答案
您需要组由常见的老年行,然后算多少每个组中:
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
要然后将其转换成一个数组,这样做在PHP中:
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.
这篇关于MySQL的计数频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!