我有4张这样的表:
每个订单都有一个客户,可以是type1
或type2
。
在订单表格中我有customerId
,在customer_type中我有orderId
以及该订单的客户类型。
我的问题是如何只通过一个选择与客户订购?
答案 0 :(得分:0)
您可以使用union all
运算符将type1
和type2
作为一个表,从那里只是一系列join
s:
SELECT order_details, customer_details
FROM orders o
JOIN customer_type ct ON o.customerId = ct.id
JOIN (SELECT *, 'type1' AS type
FROM type1
UNION ALL
SELECT *, 'type2' AS type
FROM type2) t ON t.type = ct.type