GROUP BY 语句怎么用php输出来类
数据库 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`;<br />$rs = mysql_query($sql);<br />while($r<span>@本文来*源gaodai#ma#com搞*!代#%^码$网*</span><textarea>搞gaodaima代码</textarea>ow = mysql_fetch_assoc($rs)) {<br /> $res[$row['chengji']] = $row['count'];<br />}<br />print_r($res);<br />
可以得到类似这样的数组
Array<br />(<br /> [及格] => 4<br /> [不及格] => 1<br /> [良好] => 3<br />)<br />
然后
$res[''] = '数量';<br />$head = array('', '不及格', '及格', '良好');<br />echo '<table>';<br />echo '<tr><th>' . join('</th><th>', $head) . '</th></tr>';<br />echo '<tr>';<br />foreach($head as $v) echo '<td>' . (isset($res[$v]) ? $res[$v] : '') . '</td>';<br />echo '</tr></table>';<br />
——解决方案——————–
<br /><?php<br />date_default_timezone_set("PRC");<br /><br />echo "当前月份是".date("m")."\n";<br />echo "本月初日期是".date("m")."-01\n";<br />echo "本月未日期是".date("m")."-".date("t")."\n";<br />?><br />