Himanshu Bhardiya
Himanshu Bhardiya

Reputation: 1057

custom join query to get result from 4 tables

I have 4 tables in mysql database like following:

Table : videos

id    name
1     name1
2     name2    

Table : channels

id    name       media_image_id
1     channel1   5

Table : channel_has_videos

channel_id    video_id
1             2

Table : media_image

id    filename
5     filefive.jpg

Here I would like to get videos.name + media_image.filename of channel where video belongs to channel.

So here desire output will be:

id    name    filename
2     name2   filefive.jpg 

I tried but did not get exact what i want. Thanks in advance.

Upvotes: 0

Views: 59

Answers (1)

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

You need to join all the tables. The result will return if data is found across all the tables

select
v.id.
v.name,
mi.filename
from channel_has_videos chv 
join channels c on c.id = chv.channel_id
join videos v on v.id = chv.video_id
join media_image mi on mi.id = c.media_image_id

Upvotes: 1

Related Questions