GROUP BY 語句怎麼用php輸出來類

來源:互聯網
上載者:User
資料庫 tabel 中有 `chengji` 欄位 內容為 及格 不及格 良好 三種值
頁面顯示出三種值的統計數
我用
SELECT `chengji`,count(*) FROM `table` GROUP BY `chengji`

在Mysql 中可以出來 可是要怎麼顯示到php檔案中那

頁面樣子為
| 不及格 | 及格 | 良好 |
數量 | | | |


回複討論(解決方案)

$sql = SELECT `chengji`,count(*) AS count FROM `table` GROUP BY `chengji`
在php中把mysql_query($sql);的結果列印出來看看

$sql = SELECT `chengji`,count(*) AS count FROM `table` GROUP BY `chengji`
在php中把mysql_query($sql);的結果列印出來看看

結果是Resource id #5

$sql = SELECT `chengji`,count(*) AS count FROM `table` GROUP BY `chengji`
在php中把mysql_query($sql);的結果列印出來看看

用mysql_fetch_array

結果為Array ( [0] => 及格 [type] => 及格 [1] => 4 [count] => 4 )

用mysql_fetch_array
結果為Array ( [0] => 及格 [type] => 及格 [1] => 4 [count] => 4 )
while($row = mysql_fetch_array()){}


用mysql_fetch_array
結果為Array ( [0] => 及格 [type] => 及格 [1] => 4 [count] => 4 )
while($row = mysql_fetch_array()){}
我明白了 這樣和資料庫中查詢出的格式的一樣的
可是我想在頁面中做一個固定的table 和資料庫中的格式不一樣

$sql = SELECT `chengji`,count(*)  AS count FROM `table` GROUP BY `chengji`;$rs = mysql_query($sql);while($row = mysql_fetch_assoc($rs)) {  $res[$row['chengji']] = $row['count'];}print_r($res);
可以得到類似這樣的數組
Array(    [及格] => 4    [不及格] => 1    [良好] => 3)

然後
$res[''] = '數量';$head = array('', '不及格', '及格', '良好');echo '';echo '';echo '';foreach($head as $v) echo '';echo '
' . join('', $head) . '
' . (isset($res[$v]) ? $res[$v] : '') . '
';

再問個小問題 怎麼用date() 擷取 當月 月初 和 月末 日期

再問個小問題 怎麼用date() 擷取 當月 月初 和 月末 日期


  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.