我让 Gordons 查询更简单一些。它现在完全符合您的要求。
SELECT
`name`
FROM
(
(SELECT 'project' as `type`, `name`, `id` as `projectid` FROM `Project`)
UNION ALL
(SELECT 'todo' as `type`, `name`, `projectid` FROM `Todo`)
) as `combined`
ORDER BY
`project_id`, `type`
PHP 只获取所有行的列表:
$q = $db->query("SELECT `name` FROM ((SELECT 'project' as `type`, `name`, `id` as `project_id` FROM `projects`) union all (SELECT 'todo' as `type`, `name`, `project_id` FROM `todos`)) as `combined` ORDER BY `project_id`, `type`");
while($row = $q->fetch_object()) {
echo $row->name . '<br />';
}
PHP 使用“复杂”查询获取嵌套列表:
$q = $db->query("SELECT `name`, `type` FROM ((SELECT 'project' as `type`, `name`, `id` as `project_id` FROM `projects`) union all (SELECT 'todo' as `type`, `name`, `project_id` FROM `todos`)) as `combined` ORDER BY `project_id`, `type`");
echo '<ul>';
$needToBeClosed = false;
while($row = $q->fetch_object()) {
if($row->type == 'project' AND $needToBeClosed) {
echo '</ul></li>';
$needToBeClosed = false;
}
echo '<li>' . $row->name;
if($row->type == 'project') {
echo '<ul>';
$needToBeClosed = true;
} else {
echo '</li>';
}
}
if($needToBeClosed) {
echo '</ul></li>';
}
echo '</ul>';
但正如您所看到的,您在查询中尝试做的越多。更多的 PHP 需要以简单的方式使用它。所以你需要在 SQL 和 PHP 之间找到一个平衡点来获得最好的代码。我通常不会采用上述方法,而只是执行多个查询,如下所示:
PHP 在没有“复杂”查询的情况下获取嵌套列表:
$projects = $db->query('SELECT * FROM `projects`');
echo '<ul>';
while($project = $projects->fetch_object()) {
echo '<li>' . $project->name . '<ul>';
$todos = $db->query('SELECT * FROM `todos` WHERE `project_id` = ' . $project->id);
while($todo = $todos->fetch_object()) {
echo '<li>' . $todo->name . '</li>';
}
echo '</ul></li>';
}
echo '</ul>';
您仍然需要根据自己的需要(表名等)修改查询。