Hi guys im trying to do something like
SELECT * from accounttable, peopletable, companytable
WHERE if accounttable.account_type = company
JOIN companytable
WHERE companytable.id = accounttable.company_id
ELSE IF accounttable.account_type = = person
JOIN peopletable
WHERE peopletable.id = accounttable.person_id
I'm sorry its a bit sqlenglish but I really dont know how to write it out!
解决方案
What about something like:
SELECT *
from accounttable
left join peopletable ON (accounttype = 'person' AND peopletable.id = accounttable.person_id)
left join companytable ON (accounttype = 'company' AND companytable.id = accounttable.company_id)
I'll join against both tables, so you'll have fields of all three tables in the output. The fields from peopletable will be NULL if accounttype != 'person', and those of companytable will be NULL where accounttype != 'company'.
You can find more on the LEFT JOIN syntax in places like here...