Php数据库信息排序

时间:2014-04-10 13:12:57

标签: php sql

我想让php给我一些关于我的参与者名单的统计数据。我需要知道每个联盟的每个状态有多少人(来自) 如果你不知道所有的隶属关系和stati,有没有简单的方法呢?

<?php
include 'includes/config.php';

$sql="SELECT * FROM participants ORDER BY `from`, `status`";

$self = mysql_query($sql);

echo "<table><tr><th>Name</th><th>Affiliation</th><th>Status</th></tr>";

while ($row = mysql_fetch_array($self)) {
   echo "<tr><td>".$row['name']."</td><td>".$row['from']."</td><td>".$row['status']."</td></tr>";
}
echo "</table>";
?>

数据库的结构是 Name - Affiliation - Status

所以注册的每个人都会被放入 Bob - Minnesota University - Phd Student

现在我想要一份所有隶属关系和每个联盟的列表,我需要一份所有站点的列表以及有多少人分享它们。

所以喜欢: Minnesota University Phd Student: 5 Professor: 12 Bachelor Student: 4 Caltech Phd Student: 1 Professor: 14 Bachelor Student:32

1 个答案:

答案 0 :(得分:0)

这可能是这样的:

$sql="SELECT count(*) as total_status, * FROM participants 
    GROUP BY `status`, `from`  ORDER BY `from`";

    $self = mysql_query($sql);

    echo "<table>
            <tr>
            <th>Name</th>
            <th>Affiliation</th>
            <th>Status</th>
            <th>Total Status</th>
            </tr>";

    while ($row = mysql_fetch_array($self)) {
       echo "<tr>
                <td>".$row['name']."</td>
                <td>".$row['from']."</td>
                <td>".$row['status']."</td>
                <td>".$row['total_status']."</td>
            </tr>";
    }
    echo "</table>";