我有以下表格:
课程

+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| c_id        | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| c_name      | varchar(255) | NO   |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+

物品
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| a_id        | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| a_name      | varchar(255) | NO   |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+

课程文章
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| ca_id       | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| a_id        | bigint(20)   | NO   |     | NULL    |                |
| c_id        | bigint(20)   | NO   |     | NULL    |                |
| t_id        | bigint(20)   | NO   |     | NULL    |                |
| sort_order  | int(11)      | NO   |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+

学期(或学期)
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| t_id        | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| t_name      | varchar(255) | NO   |     | NULL    |                |
| c_id        | bigint(20)   | NO   |     | NULL    |                |
| sort_order  | int(11)      | NO   |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+

我需要提供这些表格中的数据如下:
课程名称:现代天然气开采
文章:
简介
瓦斯抽采技术综述
第一学期:传统技术
煤矿瓦斯
另一种传统技术
另一篇不在任期内的文章
另一个
术语2:非常规技术
地下煤气化
煤层气
水力压裂
结论
我知道这些数据可能会从嵌套/树中受益。但这是我必须要做的。正如你所见,文章可以属于一个术语,也可以是独立的。
对于如何根据示例有效地查询上面的内容以输出,我有些困惑。
<?php
// UPDATED with UNION as suggested
try {
$stmt = $dbh->prepare(
    "SELECT ca . * , a.a_name, t.t_name
FROM Course_Article AS ca
LEFT JOIN Article AS a ON a.a_id = ca.ca_id
LEFT OUTER JOIN Term AS t ON t.t_id = ca.t_id
WHERE ca.c_id = '2'
UNION SELECT te.t_name, te.t_id, te.c_id
FROM Term AS te
WHERE te.t_id = '2'");

$last_term_id = -1;

$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  if ($last_term_id != $row['t_id']) {
    echo "<b>" . $row['t_name'] . "</b><br />";
    $last_term_id = $row['t_id'];
  }
  echo $row['a_name'] . "<br />";
  }
 } catch(PDOException $e) {
  echo 'Error : '. $e->getMessage();
  exit();
  }
?>

谢谢你

最佳答案

所以这里有一个关于如何使用union的想法:第一部分得到所有的术语(也是空的术语)和它们下面的文章,第二部分得到所有属于课程但不属于任何术语的附加文章。希望我没有在这里包含太多的逻辑错误。

select t.t_name, ca.*, a.a_name
from Term t
left outer join Course_Article ca ON ca.t_id = t.t_id
left outer join Article a ON a.a_id = ca.a_id
where t.c_id = '2'

UNION

select null as t_name, ca.*, a.a_name
from Course_Articles ca
left outer join Article a ON a.a_id = ca.a_id
where ca.c_id = '2' and ca.t_id is null

此外,如果您需要对结果进行排序(如果排序顺序在课程文章和术语之间是通用的,则不需要),则可以对此进行扩展(将需要的任何列添加到最终结果的主选择中):
select t_name, a_name, sort_column
FROM
(
    select t.t_name, ca.*, a.a_name, t.sort_order as sort_column
    from Term t
    left outer join Course_Article ca ON ca.t_id = t.t_id
    left outer join Article a ON a.a_id = ca.a_id
    where t.c_id = '2'

    UNION

    select null as t_name, ca.*, a.a_name, ca.sort_order as sort_column
    from Course_Articles ca
    left outer join Article a ON a.a_id = ca.a_id
    where ca.c_id = '2' and ca.t_id is null
) dt
order by sort_column ASC

关于php - 从3个表中选择属于和不属于的mysql数据?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33102571/

10-14 05:18