有人可以告诉我你将如何输出这个?输出就像位置M F总计。
SELECT location, SUM(IF(gender='M',1,0)) AS M,
SUM(IF(gender='F',1,0)) AS F, COUNT(*) AS total
GROUP by location;
取自 - http://dev.mysql.com/tech-resources/articles/wizard/page3.html
由于
修改
如何获得像
这样的答案location M F total
Boston 1 2 3
Cagliari 1 2 3
London 1 1 2
Manchester 1 1 2
Marseille 1 2 3
Milano 2 0 2
New York 2 2 4
Paris 1 1 2
Roma 3 0 3
答案 0 :(得分:1)
循环遍历它并将其回显到表中:
<?php
$q="SELECT location, SUM(IF(gender='M',1,0)) AS M,
SUM(IF(gender='F',1,0)) AS F, COUNT(*) AS total
GROUP by location";
$query=mysql_query($q);
echo '<table>';
while($r=mysql_fetch_assoc($query)){
echo '<tr>';
echo '<td>'.$r['location'].'</td>';
echo '<td>'.$r['M'].'</td>';
echo '<td>'.$r['F'].'</td>';
echo '<td>'.$r['total'].'</td>';
echo '</tr>';
}
echo '</table>';
?>