PHP 使用 3 个连接从 2 个表输出数据

PHP Outputting data from 2 tables with 3 joins

希望能帮到你,

我有 3 个表
article
article_keywords_link
keywords

它们是连在一起的,我想实现1篇文章显示在页面上,但显示所有链接到该文章的相关关键字。

我还没有格式化这个页面,我只是想让功能正常工作。

SELECT article.article_id, article.article_title, article.article_content, keywords.keyword_name
FROM article
JOIN article_keyword_link ON article.article_id = article_keyword_link.article_id
INNER JOIN keywords ON article_keyword_link.keyword_id = keywords.keyword_id

运行 在 Phpmyadmin 中进行上述查询,我​​收到了这些结果

Query Output from PhpmyAdmin
+------------+---------------+-------------------+--------------+
| article_id | article_title |  article_content  | keyword_name |
+------------+---------------+-------------------+--------------+
|          1 | Article 1     | Article 1 content | CCU          |
|          1 | Article 1     | Article 1 content | WIFI         |
|          1 | Article 1     | Article 1 content | Network      |
|          2 | Article 2     | Article 2 content | Passenger    |
|          2 | Article 2     | Article 2 content | Application  |
+------------+---------------+-------------------+--------------+

表格的填充方式如下

Table: article
+------------+---------------+-------------------+
| article_id | article_title |  article_content  |
+------------+---------------+-------------------+
|          1 | Article 1     | Article 1 content |
|          2 | Article 2     | Article 2 content |
+------------+---------------+-------------------+
Table: article_keywords_link
+------------+------------+
| article_id | keyword_id |
+------------+------------+
|          1 |          1 |
|          1 |          2 |
|          2 |          3 |
|          2 |          4 |
|          1 |          7 |
+------------+------------+
Table: keywords
+------------+--------------+
| keyword_id | keyword_name |
+------------+--------------+
|          1 | CCU          |
|          2 | WIFI         |
|          3 | Passenger    |
|          4 | Application  |
|          5 | Software     |
|          6 | Hardware     |
|          7 | Network      |
+------------+--------------+

页面来源:

<?php

include 'includes/db.php';

// Create connection
$conn = new mysqli($host, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "

SELECT article.article_id, article.article_title, article.article_content, keywords.keyword_name
FROM article
JOIN article_keyword_link ON article.article_id = article_keyword_link.article_id
INNER JOIN keywords ON article_keyword_link.keyword_id = keywords.keyword_id

";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "Article ID: " . $row["article_id"] . "<br>";
        echo "Article Title: " . $row["article_title"] . "<br>";
        echo "Article Content: " . $row["article_content"] . "<br>"
;
    }
} else {
    echo "0 results";
}
$conn->close();
?>

页面输出:

Article ID: 1
Article Title: Article 1
Article Content: Article 1 content
Article ID: 1
Article Title: Article 1
Article Content: Article 1 content
Article ID: 1
Article Title: Article 1
Article Content: Article 1 content
Article ID: 2
Article Title: Article 2
Article Content: Article 2 content
Article ID: 2
Article Title: Article 2
Article Content: Article 2 content

所以我面临两个问题。
1. 如何只检索一篇文章显示在页面上,同时显示与该文章相关的所有关键字?
2. 如何调整我的源代码以显示关键字?如果我在 keyword_name 的循环中添加新的 echo,则页面不会加载。

如有任何帮助,我们将不胜感激

快完成了,试试这个。

SELECT article.article_id, article.article_title, article.article_content, group_concat(keywords.keyword_name) as keywords
FROM article
JOIN article_keyword_link ON article.article_id = article_keyword_link.article_id
INNER JOIN keywords ON article_keyword_link.keyword_id = keywords.keyword_id
GROUP BY article.article_id

有关用于实现此目的的功能的信息。请看

https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html

此外,如果您只想要一篇文章,请将 limit 1 添加到查询的末尾。如果你想要 newest/oldest 你会为最新添加 ORDER BY article_id DESC LIMIT 1 和为最旧添加 ORDER BY article_id ASC LIMIT 1