简体   繁体   English

将oracle转换为mysql以选择listagg并退出联接

[英]Converting oracle to mysql to select listagg and left join

Please, I have the below oracle query to select post and list all share image attached to a post and at the same time join user table and post table. 请在下面的oracle查询中选择帖子,并列出所有附加到帖子的共享图像,并同时加入用户表和帖子表。 My problem now is to run this query in MySQL. 我现在的问题是在MySQL中运行此查询。 I have tried so many modifications but it kept giving me errors can someone help out? 我已经尝试了很多修改,但是它不断给我带来错误,有人可以帮忙吗?

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'

FULL DEMO WITH TABLE http://sqlfiddle.com/#!9/9c39ef/2 带表的完整演示http://sqlfiddle.com/#!9/9c39ef/2

The mysql equivalent of listagg is group_concat - 相当于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;

There is no with(cte) in mysql until version 8 but i think you can replace this in your query with a sub query to create i. 直到版本8,mysql中都没有with(cte),但是我认为您可以在查询中用子查询替换它来创建i。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM