这不起作用,我怎样才能构建这个查询? 根据debtor_user_type字段的值,我需要加入相应的表
SELECT * FROM invoice_headers
CASE
WHEN invoice_headers.debtor_user_type = 0
THEN
LEFT JOIN user_info
ON invoice_headers.debtor_id = user_info.user_id
ELSE
LEFT JOIN company_info
ON invoice_headers.debtor_id = company_info.company_id
END
答案 0 :(得分:3)
在ON子句中使用 LEFT OUTER JOIN 条件。
试试这个:
SELECT *
FROM invoice_headers A
LEFT OUTER JOIN user_info B ON A.debtor_id = B.user_id AND A.debtor_user_type = 0
LEFT OUTER JOIN company_info C ON A.debtor_id = C.company_id AND A.debtor_user_type <> 0