希望您能提供帮助,

我有3个表格articlearticle_keywords_linkkeywords

他们在一起,我想在页面上显示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添加新的回显,则不会加载页面。

任何帮助,将不胜感激

最佳答案

您快到了,试试看。

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。如果您想要最新/最旧的信息,则可以添加ORDER BY article_id DESC LIMIT 1表示最新信息,并添加ORDER BY article_id ASC LIMIT 1表示最新信息。

关于php - PHP从3个联接的2个表输出数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61030069/

10-16 09:32