将oracle转换为mysql以选择listagg并离开join

时间:2017-11-16 22:24:14

标签: php mysql sql

请,我有以下oracle查询选择发布并列出附加到帖子的所有共享图像,同时加入用户表和帖子表。我现在的问题是在MySQL中运行此查询。我已经尝试了很多修改,但它一直给我错误,有人可以帮忙吗?

SELECT 
        post_image_id,
        LISTAGG(photo_url, ", ") WITHIN GROUP (ORDER BY photo_url) imgs
        FROM social_post_photos
        GROUP BY post_image_id;

        WITH i AS (
          SELECT post_image_id,
            LISTAGG(photo_url, ", ") WITHIN GROUP (ORDER BY photo_url) imgs
          FROM social_post_photos
          GROUP BY post_image_id
        )  

        SELECT sp.post_body_message, i.imgs AS images
        FROM social_posts sp
        LEFT JOIN vendor_account va
           ON sp.vendor_owner_id = va.eu_vendor_id
        LEFT JOIN i
           ON sp.social_page_id = i.post_image_id

           WHERE sp.social_page_id = 'page1'
           AND sp.vendor_owner_id = 'v100'

http://sqlfiddle.com/#!9/9c39ef/2的完整演示

1 个答案:

答案 0 :(得分:0)

listagg的mysql等价物是group_concat -

SELECT 
        post_image_id,
        group_concat(photo_url ORDER BY photo_url) imgs
        FROM social_post_photos
        GROUP BY post_image_id;

在版本8之前,mysql中没有(cte),但我认为你可以在查询中用子查询替换它来创建i。