【问题标题】:SQL GROUP CONCAT and LEFT JOIN return only one result from databaseSQL GROUP CONCAT 和 LEFT JOIN 只从数据库返回一个结果
【发布时间】:2018-05-07 09:08:32
【问题描述】:

我正在尝试运行 sql 查询,该查询可以返回所有用户帖子,如果用户在提交帖子时附加了图片,还可以加入帖子图片。但是使用下面的代码,当post id 存在于post image id 表上时,它只会返回来自post table 的数据,否则它将忽略所有没有图像的帖子。我如何编写一个查询返回有图像和没有图像的帖子?

下面是我的表结构

表 vendor_account

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

表 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

表格 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 查询时,它会返回如下结果并忽略其他没有附加图像的帖子

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]

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

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

【问题讨论】:

    标签: mysql sql phpmyadmin group-concat


    【解决方案1】:

    由于您是从social_post_photos 中选择的,因此您需要将您的联接更改为social_postsRIGHT JOIN,以便从social_posts 获取所有记录。由于我厌恶RIGHT JOINs,并且还没有找到使用它的充分理由,我建议您更改表格的顺序以从social_postsLEFT JOIN 中选择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
    

    注意在执行此操作时,您还需要选择(和分组依据)p.post_id 而不是 im.post_image_id

    【讨论】:

      【解决方案2】:

      LEFT JOIN 时,将右侧表条件从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 版本上执行(除非在兼容模式下),可能会在较旧的 MySQL 版本上返回不可预测的结果。一般的 GROUP BY 规则说:如果指定了 GROUP BY 子句,则 SELECT 列表中的每个列引用必须要么标识一个分组列,要么是一个集合函数的参数!

      【讨论】:

      • 仍然只返回两个数据
      【解决方案3】:

      你可以试试下面的代码。

      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

      【讨论】:

        【解决方案4】:

        来自您的帖子:

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

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

        为了获得这两行,您需要LEFT JOIN 而不是INNER JOIN 最后一个表。此外,Where 条件只会选择一个帖子和一个供应商,我不确定这是否是您想要的。

        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
        

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

        【讨论】:

          猜你喜欢
          • 2019-09-06
          • 2021-09-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-04-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多