left/right/inner join on mysql statement -
i have following sql query :
select band.id bid, band.name, bandinfo.summary, bandimage.url bandimage, user.username, user.online, userimage.url userimage bands band inner join band_info bandinfo on band.id = bandinfo.bid left join band_images bandimage on band.id = bandimage.bid left join band_followers follower on follower.bid = band.id right join users user on user.id = follower.uid inner join user_info userinfo on userinfo.uid = user.id left join user_images userimage on user.id = userimage.uid ((band.activated = 1) , (user.activated = 1)) , (bandinfo.language = 'fr') order band.name
'users' table contains users (one row = 1 user)
'user_info' table contains info user (one row = 1 user)
'user_images' table contains image users (one row = 1 user)
'bands' table contains music bands (one row = 1 band)
'band_info' table contains info band (multiple row 1 band due language)
'band_images' table contains image bands (one row = 1 band)
'band_followers' table contains relations between users , bands (one row = 1 relation there many row containing same band id not same user id)
i retrieve bands info (inner join) if band id isn't in band_followers table. want retrieve bands when band id in band_followers table inner join on user id user's info (inner join) if doesn't have image.
my problem join keyword.. don't know if have use left or right join
thanks !
update answer :
select band.id bid, band.name, bandinfo.summary, bandimage.url bandimage, user.username, user.online, userimage.url userimage bands band inner join band_info bandinfo on band.id = bandinfo.bid left join band_images bandimage on band.id = bandimage.bid left join band_followers follower on band.id = follower.bid left join users user on user.id = follower.uid , user.activated = 1 left join user_info userinfo on userinfo.uid = user.id left join user_images userimage on user.id = userimage.uid (band.activated = 1) , (bandinfo.language = 'fr') order band.name
thank all
you should use left join
before users
, user_info
, before band_followers
. left join
means return rows of previous tables if don't match in table you're joining with. right join
means return rows of table you're joining with. if use right join users
, you'll users, not users following band.
you should put test user.activated = 1
on
clause of left join users
. otherwise, you'll filter out bands have no followers, because don't have followers user.activated
.
Comments
Post a Comment