7

同様の質問を確認しましたが、正確な質問には役立ちませんでした。

したがって、私のテーブルは次のようになります。

id age
1  30
2  36
3  30
4  52
5  52
6  30
7  36

等..

年齢の頻度を数える必要があります。

age freq
30   2
36   3
52   2

この周波数を取得するにはどうすればよいですか? この後、そのデータを操作する必要があるので、配列を使用する必要があるのでしょうか? ありがとう!

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]}]);";
            }

        ?>

目標はチャートを作成することです

4

4 に答える 4

12

一般的な年齢で行をグループ化し、各グループに含まれる数を数えます。

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

それを配列に変換するには、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];
}

$frequencies という名前の連想配列ができました。キーは年齢、値は頻度です。

于 2012-11-15T13:11:10.270 に答える
2
select age, name, count(*) freq from user_age group by age

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

于 2012-11-15T13:20:58.457 に答える
0
select age, count(*) freq from mytable group by age
于 2012-11-15T13:11:24.467 に答える
0
select age, count(id) as freq from table group by age
于 2012-11-15T13:11:28.357 に答える