SQL GROUP CONCAT 和 LEFT JOIN return 只有一个来自数据库的结果

SQL GROUP CONCAT and LEFT JOIN return only one result from database

我正在尝试 运行 sql 查询,该查询可以 return 所有用户 post 并且如果用户在提交图片时附加了图片,还可以加入 post 图片post。但是使用下面的代码,当 post id 存在于 post image id table 时,它仅 return 来自 post table 的数据,否则它将忽略所有 post 而不图片。我如何编写 return post 有图像和无图像的查询?

下面是我的table结构

Table vendor_account

eu_vendor_id | uname |  mypagename
-------------|-------|--------------
v801         | peter |  pageA
v900         | john  |

Table social_posts

social_page_id  |  post_id | vendor_owner_id | post_body_message
----------------|----------|-----------------|------------------------------
pageA           |  p100    |  v801            | This is post without image
pageA           |  p101    |  v801            | This is post with 2 images
pageA           |  p102    |  v801            | This is post with 1 image

Table social_post_photos

img | post_image_id | photo_url
----|---------------|---------------
1   | p101          | image1.png
2   | p101          | image2.png
3   | p102          | image01.png

我的SQL查询

SELECT 
im.post_image_id, p.post_body_message,
group_concat(im.photo_url ORDER BY im.photo_url) imgs
FROM social_post_photos im

LEFT JOIN social_posts p
ON p.post_id = im.post_image_id

INNER JOIN vendor_account v
ON p.vendor_owner_id = v.eu_vendor_id

WHERE p.social_page_id = 'pageA'
AND p.vendor_owner_id = 'v801'

GROUP BY im.post_image_id

当前结果 当我 运行 上述 sql 查询时,它将 return 结果如下,并忽略其他 post 没有附加图像的

post_image_id  |  post_body_message         |  photo_url
---------------|----------------------------|-------------------------
p101           | This is post with 2 images | [IMAGE1.PNG],[IMAGE2.PNG]
p102           | This is post with 1 image  | [IMAGE01.PNG]

我的预期结果 我想要一个可以 return 所有数据的查询,即使它没有图像,因为我使用 LEFT JOIN 因为不是强制要求 post 将附有照片

post_image_id  |  post_body_message         |  photo_url
---------------|----------------------------|-------------------------
p101           | This is post with 2 images | [IMAGE1.PNG],[IMAGE2.PNG]
p102           | This is post with 1 image  | [IMAGE01.PNG]
p100           | This is post without image | null

LEFT JOIN时,将右边的table条件从WHERE子句移动到ON子句(否则你会得到常规的INNER JOIN结果):

SELECT 
im.post_image_id, p.post_body_message,
group_concat(im.photo_url ORDER BY im.photo_url) imgs
FROM social_post_photos im

LEFT JOIN social_posts p
ON p.post_id = im.post_image_id
AND p.social_page_id = 'pageA'
AND p.vendor_owner_id = 'v801'

INNER JOIN vendor_account v
ON p.vendor_owner_id = v.eu_vendor_id


GROUP BY im.post_image_id

请注意,您的 group by 无效。它不会在较新的 MySQL 版本上执行(除非在兼容模式下),可能 return unpredictable 结果与旧的 MySQL 版本。一般的 GROUP BY 规则说:如果指定了 GROUP BY 子句,SELECT 列表中的每个列引用必须标识分组列或者是集合函数的参数!

由于您 select 来自 social_post_photos,您需要将连接更改为 social_postsRIGHT JOIN,以便从 social_posts。由于我讨厌 RIGHT JOINs,并且还没有找到使用它的充分理由,我建议您将表格的顺序从 social_posts 和 [=17 更改为 select =] 到 social_post_photos:

SELECT 
p.post_id, p.post_body_message,
group_concat(im.photo_url ORDER BY im.photo_url) imgs
FROM social_posts p

LEFT JOIN social_post_photos im
ON im.post_image_id = p.post_id
INNER JOIN vendor_account v
ON p.vendor_owner_id = v.eu_vendor_id

WHERE p.social_page_id = 'pageA'
AND p.vendor_owner_id = 'v801'

GROUP BY p.post_id, p.post_body_message

N.B。在执行此操作时,您还需要 select(并分组依据)p.post_id 而不是 im.post_image_id.

来自你的post:

INNER JOIN vendor_account v
ON p.vendor_owner_id = v.eu_vendor_id

这部分会将您的结果减少到最多 2 个,因为您只有 1 个供应商 ID (peter),这是您唯一的结果(我假设这是您得到的单行)。

为了获得这两行,您需要 LEFT JOIN 而不是 INNER JOIN 最后 table。此外,Where 条件只会 select 一个 post 和一个供应商,我不确定那是你想要的。

SELECT 
im.post_image_id, p.post_body_message,
group_concat(im.photo_url ORDER BY im.photo_url) imgs
FROM social_post_photos im

LEFT JOIN social_posts p
ON p.post_id = im.post_image_id

LEFT JOIN vendor_account v
ON p.vendor_owner_id = v.eu_vendor_id

-- -- I DON'T KNOW IF THIS IS RIGHT?!
-- WHERE p.social_page_id = 'pageA'
-- AND p.vendor_owner_id = 'v801'

GROUP BY im.post_image_id

这里有一个 fiddle,里面有你需要的一切:http://sqlfiddle.com/#!9/e7ce30/17

你可以试试下面的代码。

SELECT 
im.post_image_id, p.post_body_message,
group_concat(im.photo_url ORDER BY im.photo_url) imgs
FROM social_posts p

LEFT JOIN social_post_photos im
ON p.post_id = im.post_image_id

INNER JOIN vendor_account v
ON p.vendor_owner_id = v.eu_vendor_id

WHERE p.social_page_id = 'pageA'
AND p.vendor_owner_id = 'v801'

GROUP BY im.post_image_id