sql - Dynamic Join of Tables (MySQL) -


i'm trying figure out right way join tables in given setup:

table0: col1 col2 col3 colx coly

col1-3 foreign keys 3 different tables - may or may not have values (e.g. col1 may null, or both col 2 , 3 or none). i'm trying build select query joins table 1 - 3 if , if value exists.

i hope explained well.

it sounds you're asking left join (or kind of outer join):

select table0.*, user.*, show.* table0 left join user on user.id=table0.userid left join show on show.id=table0.showid 

a typical result might be

table0.id  table0.userid  table0.showid  user.id  user.name  show.id  show.name 1          1              null           1        bob        null     null 2          null           1              null     null       1        flintstones 

Comments