可能的选择查询

时间:2014-10-17 09:17:42

标签: mysql

我有4张这样的表:

  • 顺序
  • 的customer_type
  • TYPE1
  • TYPE2

每个订单都有一个客户,可以是type1type2。 在订单表格中我有customerId,在customer_type中我有orderId以及该订单的客户类型。

我的问题是如何只通过一个选择与客户订购?

1 个答案:

答案 0 :(得分:0)

您可以使用union all运算符将type1type2作为一个表,从那里只是一系列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