基于两个表输出数据

时间:2015-11-24 07:54:18

标签: php mysql

我需要根据另外两个表输出一个表,如下所示:

table and output

案例:有两个表" tbl_schedule"和" tbl_report"

这是我的剧本:

$sql = mysql_query("SELECT*, count(*) as schedule_date FROM mst_schedule WHERE schedule_date LIKE '%$date' GROUP BY schedule_account") or die (mysql_error());
while ($data = mysql_fetch_array($sql)) {
$account = schAccount($data['schedule_account']);
$sql2 = mysql_query("SELECT * FROM trn_reportsch WHERE schedule_id='$data[schedule_id]' GROUP BY schedule_id");
echo "<tr>";
echo "<td>".ucfirst($account['admin_fullname'])."</td>";
while ($data2 = mysql_fetch_array($sql2)) {
echo "<td>".$data2['rating']."</td>";
}
echo "<td>".$data['schedule_date']."</td>";
echo "</tr>";
}

到目前为止,我还没有获得所需的输出。我该如何更改脚本?

1 个答案:

答案 0 :(得分:0)

您的代码几乎是正确的。

添加以下行:

    $sql = mysql_query("SELECT*, count(*) as schedule_date FROM mst_schedule WHERE schedule_date LIKE '%$date' GROUP BY schedule_account") or die(mysql_error());
while ($data = mysql_fetch_array($sql)) {
    $account = schAccount($data['schedule_account']);
    $sql2 = mysql_query("SELECT * FROM trn_reportsch WHERE schedule_id='$data[schedule_id]' GROUP BY schedule_id");
    echo "<tr>";
    echo "<td>" . ucfirst($account['admin_fullname']) . "</td>";

    $bad = $good = $vGood = 0; // <-- ADD THIS LINE

    while ($data2 = mysql_fetch_array($sql2)) {
        if($data2['rating'] <=2){ // BAD
            $bad++;
        } else if($data2['rating'] <= 3){ // GOOD
            $good++;
        } else if($data2['rating'] > 3){ // VERY GOOD
            $vGood++;
        }
    }
    echo "<td>" . $bad . "</td>"; // Display the final value for bad
    echo "<td>" . $good . "</td>"; // Display the final value for good
    echo "<td>" . $vGood . "</td>"; // Display the final value for very good
    echo "<td>" . $data['schedule_date'] . "</td>";
    echo "</tr>";
}