嗨,伙计们,我想做点什么

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

我很抱歉它有点英语,但我真的不知道如何写出来!

最佳答案

比如说:

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)

我将对这两个表进行联接,这样就可以在输出中包含所有三个表的字段。如果peopletable,则NULL中的字段为accounttype != 'person',如果companytable,则NULL中的字段为accounttype != 'company'
您可以在here这样的地方找到关于LEFT JOIN语法的更多信息。

08-04 04:51