我有四张表products
,product_histories
,vendor_invoices
和invoices
这是我开发的查询
SELECT p.product_id, product_name, vendor_name FROM products AS p
INNER JOIN product_histories AS ph ON p.product_id = ph.product_id
CASE
WHEN ph.history_type = "P" THEN
LEFT JOIN vendor_invoices AS vi ON link_id = vi.vi_id
WHEN ph.history_type = "S" THEN
LEFT JOIN invoices AS i ON i.invoice_id = link_id
END
ORDER BY ph_id ASC
如果ph.history_type
为P
,我应该加入vendor_invoices
,如果是S
,那么它应该加入invoices
。但它表示存在语法错误。
任何人都可以帮我解决这个问题吗?或者可以展示更好的方法来解决这个问题。