开发者

How to add GROUP_CONCAT to LEFT JOIN query?

开发者 https://www.devze.com 2023-02-05 08:35 出处:网络
I have this query (and results): select articles.article_id, articles.article_text, article_photos.photo_filename

I have this query (and results):

select articles.article_id, articles.article_text, article_photos.photo_filename
from
  articles
left join article_photos
on article_photos.article_id=articles.article_id

>>> results
1,some_text,photo1.jpg
1,some_text,photo2.jpg
1,some_text,photo3.jpg

How do I incorporate GROUP_CONCAT to this so that I get:

>>> results

1,some_text,photo1.jp开发者_开发知识库g
NULL,NULL,photo2.jpg
NULL,NULL,photo3.jpg

Basically, I have a table with articles, and related table with images. An article can have multiple images belonging to it, so I'm trying to print it on screen within while loop, and don't want text to repeat over and over when there's multiple images.


select articles.article_id, articles.article_text, group_concat(article_photos.photo_filename)
    from articles
        left join article_photos
            on article_photos.article_id=articles.article_id
    group by articles.article_id, articles.article_text

would return

1    some_text    photo1.jpg,photo2.jpg,photo3.jpg

which is not quite what you've shown in your expected results. Is this what you're asking for?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号