问题
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.jpg
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.
回答1:
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?
来源:https://stackoverflow.com/questions/4740598/how-to-add-group-concat-to-left-join-query