PHP从具有3个联接的2个表中输出数据

问题描述 投票:3回答:1

希望您能提供帮助,

我有3个表格文章article_keywords_link关键字

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

对此的任何帮助将不胜感激

php mysql join output relational
1个回答
2
投票
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作为最早的

© www.soinside.com 2019 - 2024. All rights reserved.