如何在MySQL中通过查询从照片表中为每个帖子选择照片

时间:2018-10-11 11:49:31

标签: mysql sql sqlcommand

我有两个表,我想为每个帖子显示一张照片,但是当运行查询为每张照片显示帖子时

照片表:

+------------------------------------------------------------+
|  photo_id   |    post_id    |    photo_name                |
+------------------------------------------------------------+
|     1       |        1      |    flower.jpg                |
+------------------------------------------------------------+
|     2       |        1      |    book.jpg                  |
+------------------------------------------------------------+
|     3       |        2      |    pen.jpg                   |
+------------------------------------------------------------+
|     4       |        3      |    man.jpg                   |
+------------------------------------------------------------+
|     5       |        3      |    women.jpg                 |
+------------------------------------------------------------+
|     6       |        3      |    boy.jpg                   |
+------------------------------------------------------------+

发布表格:

+--------------------------------+
|  post_id    |   post_title     |
+--------------------------------+
|     1       |   flower         |
+--------------------------------+
|     2       |   book           |
+--------------------------------+
|     3       |   pen            |
+--------------------------------+
|     4       |   man            |
+--------------------------------+
|     5       |   women          |
+--------------------------------+
|     6       |   boy            |
+--------------------------------+

使用流动的SQL查询和INNER JOIN,但我的结果显示如下:

SELECT
Photo.photo_name,
Post.post_title
FROM Photo
INNER JOIN Post
ON Photo.post_id = Post.post_id 
ORDER BY Post.post_id 

执行后的结果查询:

+-----------------------------+
|  post_title |   photo_name  |
+-----------------------------+
|     flower  |  flower.jpg   |
+-----------------------------+
|     flower  |  flower.jpg   |
+-----------------------------+
|     book    |  book.jpg     |
+-----------------------------+
|     book    |  book.jpg     |
+-----------------------------+
|     pen     |  pen.jpg      |
+-----------------------------+
|     pen     |  pen.jpg      |
+-----------------------------+
|     man     |  man.jpg      |
+-----------------------------+
|     man     |  man.jpg      |
+-----------------------------+
|     women   |  women.jpg    |
+-----------------------------+
|     women   |  women.jpg    |
+-----------------------------+
|     boy     |  boy.jpg      |
+-----------------------------+
|     boy     |  boy.jpg      |
+-----------------------------+

我在帖子表中有6篇帖子,我想为每篇帖子获得一张照片,但我的结果按照片数显示帖子

2 个答案:

答案 0 :(得分:0)

您可以使用group by。由于您尚未定义帖子中有多张照片时要使用的照片,因此我们可以获得Max()张帖子的照片:

SELECT
MAX(Photo.photo_name),
Post.post_title
FROM Photo
INNER JOIN Post
ON Photo.post_id = Post.post_id 
GROUP BY Post.post_id, Post.post_title 
ORDER BY Post.post_id 

答案 1 :(得分:0)

假设您要所有张贴至少一张照片(我不知道哪张)。因此,我将对子查询执行LEFT JOIN

SELECT pst.post_title, p.photo_name
FROM Post pst LEFT JOIN 
     Photo p
     ON p.post_id = pst.post_id AND 
        p.photo_id = (SELECT MAX(p1.photo_id)
                      FROM Photo p1
                      WHERE p1.post_id = p.post_id
                     );