php - MySQL LEFT JOIN - 如何处理记录与 PHP 不匹配的情况
问题描述
我有一个 HTML 表,其中填充了从 MySQL 返回的记录LEFT JOIN
。
这可行,但我需要处理在JOIN
. 目前,HTML 表如下图所示,不匹配的 unit_outcome 记录位于没有对应 program_outcome 的单元格中。
+----------------------------------+--------------------------------------------------+
| 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 单元格中添加一个字符串,例如“没有匹配的程序结果”。我想在我的代码中的 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>";
解决方案
您可以在 SQL 中使用IFNULL()
.
$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,
IFNULL(program_outcome.program_outcome, 'No matching program outcome.') AS 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";
推荐阅读
- modal-dialog - 物化 css valign-wrapper 在模态中不起作用
- javascript - this.props is not a function between parent and children components
- c++ - C++:尝试用等效的 STL 算法消除原始循环
- anaconda - Windows10上plaidml安装
- excel-formula - 在 VLOOKUP 中使用 COUNTIF 完成课程
- primefaces - p:datagrid 不会从视图中加载数据
- c# - 在两个表之间配置实体框架多对多关系?
- java - 在由JAVA DSA签名的openssl c ++中验证签名?
- node.js - 重新加载或只编译一个文件而不重新启动服务器 nodejs
- .htaccess - .htaccess 与子域屏蔽和 https 重定向