Mysql: How to make and read a joined query?
I have 2 tables - one with articles, and one with article related photos. Each article can have none, one or multiple photos related to it (article_id as foreign key)
Query I use to fetch data is:
SELECT articles.article_id, articles.article_text article_photos.photo_filename
FROM articles开发者_运维知识库, article_photos
WHERE article_photos.article_id = articles.article_id
And it returns to me all data + 1 photo_filename.
Question is, when 2 or more photos are related to article, how do I read them?
Thanks!
It will replicate the same row from articles
, but different row from article_photos
such as
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
2,some_text2,NULL --> no photo related
精彩评论