Karl Husten Karl Husten - 2 days ago 4
MySQL Question

Mysql select rows with same id's (3 tables)

here are my tables: blog_content

blog_media

blog_media_content:

| blog_id | media_id |
========================
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 3 | 4 |


I want to select all blog_media.uri's

where blog_media.media_id equals blog_media_content.blog_id

Answer

An inner join between blog_media and blog_media_content tables would suffice.

SELECT 
bm.uri 
FROM blog_media bm 
INNER JOIN blog_media_content bmc ON bm.media_id = bmc.media_id
WHERE bmc.blog_id =3; 

Note:

If you need any additional information from blog table then you need an additional inner join like below:

...INNER JOIN blog_table b ON bmc.blog_id = b.blog_id...

EDIT:

In order to get records for all blog_ids :

SELECT 
bm.uri 
FROM blog_media bm 
INNER JOIN blog_media_content bmc ON bm.media_id = bmc.media_id
ORDER BY bmc.blog_id;
Comments