Py学习  »  DATABASE

MySQL LEFT JOIN-如何处理记录与PHP不匹配的情况

IlludiumPu36 • 5 年前 • 1463 次点击  

我有一个HTML表,其中填充了从MySQL返回的记录 LEFT JOIN

这很有效,但我需要处理在 JOIN . 目前,HTML表如下图所示,单元输出记录不匹配,它们位于没有相应程序输出的单元格中。

+----------------------------------+--------------------------------------------------+
| program_outcome                  |  unit_outcome                                    |
+----------------------------------+--------------------------------------------------+
|                                  | unit_outcome 5, unit_outcome 7                   |
+----------------------------------+--------------------------------------------------+
| program outcome 1                | unit_outcome 2, unit_outcome 4                   |
+----------------------------------+--------------------------------------------------+
| program outcome 2                | unit_outcome 1, unit_outcome 3, unit_outcome 6   |
+----------------------------------+--------------------------------------------------+

这本身是可以的,但是我想在当前空白的program_outcome单元格中添加一个类似“No matching program outcome”的字符串。我想在我的代码中的PHP中这样做,但不知道如何处理:

$query = "SELECT MAX(unit.unit_pk) AS unit_pk,
       GROUP_CONCAT(CONCAT('<strong>',unit.unit_code,': </strong>', unit_outcome.unit_outcome) SEPARATOR '|') unit_outcomes,
       MAX(program_outcome.program_outcome) program_outcome,
       GROUP_CONCAT(unit_outcome.unit_outcome_pk) unit_outcome_pks, 
       program_outcome.program_outcome_pk,
       program_outcome.program_outcome
FROM unit
LEFT JOIN unit_unit_outcome_lookup
    ON unit_unit_outcome_lookup.unit_fk = unit.unit_pk
LEFT JOIN unit_outcome
    ON unit_outcome.unit_outcome_pk = unit_unit_outcome_lookup.unit_outcome_fk
LEFT JOIN program_outcome_unit_outcome_lookup
    ON program_outcome_unit_outcome_lookup.unit_outcome_fk = unit_outcome.unit_outcome_pk
LEFT JOIN program_outcome
    ON program_outcome.program_outcome_pk = program_outcome_unit_outcome_lookup.program_outcome_fk
GROUP BY program_outcome_pk ORDER BY cast(program_outcome as unsigned) ASC";

$result = $connection->query( $query );

echo "<table width='100%' border='1'><thead><tr><th>Program Outcomes</th><th>Unit Outcomes</th></tr></thead><tbody>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr><td>" . $row['program_outcome'] . "</td><td>" . str_replace('|', '<p>',$row['unit_outcomes']) . "</td></tr>";
}
echo "<tbody></table>";
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/56516
 
1463 次点击  
文章 [ 1 ]  |  最新文章 5 年前