如何使用带条件的sql连接多个(四个)表?

时间:2020-03-13 10:00:17

标签: mysql sql

我正在尝试创建一个SQL查询,该查询有条件地从多个表中提取数据。

我有四个表:

订单

+------+------------+------------+
| id   | date_added | currency   |
+------+------------+------------+
|   1  | 2018-07-23 |          1 |
+------+------------+------------+

order_items

+------+------------+------------+---------------+---------------+
| id   | order_id   | price      | product_id    | product_type  |
+------+------------+------------+---------------+---------------+
|   1  | 1          | 100.00     |          1    |   ticket      |
+------+------------+------------+---------------+---------------+

order_data

+------+--------------+---------------+
| id   | order_id     | ext_order_ref |
+------+--------------+---------------+
|   1  | 1            |         ABC   |
+------+--------------+---------------+

产品

+------+------------+------------+
| id   | date       | product_id |
+------+------------+------------+
|   1  | 2020-03-12 |          1 |
+------+------------+------------+
|   2  | 2020-03-18 |          2 |
+------+------------+------------+
|   3  | 2020-03-20 |          3 |
+------+------------+------------+

我需要输出以下条件的订单:

  • 行中的每个订单总计(根据具有匹配订单ID的订单项计算)
  • order_data表中与该订单匹配的“ ext_order_ref”
  • 仅包括具有特定产品类型的订单商品
  • 仅包括特定日期范围内产品的订单

首选输出如下:

+------------+------------+--------------+
| order_id   | total      | ext_order_ref|
+------------+------------+--------------+
|   1        | 100        |          ABC |
+------------+------------+--------------+

我当前的查询基本上是这样的;请指教

SELECT
orders.id as order_id,
SUM(order_items.price) as total,
order_data.ext_order_ref

FROM orders

INNER JOIN order_data
ON orders.id = order_data.ext_order_ref

RIGHT JOIN order_items
ON orders.id = order_items.order_id

LEFT JOIN products
ON order_items.product_id = products.product_id
WHERE order_items.product_type = 'ticket' AND products.date BETWEEN '2020-03-12' AND '2020-03-18'

GROUP BY orders.id

几乎可以,但是效果不佳。日期特别引起问题。

谢谢!

2 个答案:

答案 0 :(得分:0)

首先确定要查询的行驶表,然后根据行驶表形成查询。 驱动表是其他表从中联接的一个主表。

More information on driving tables from askTom

在您的情况下,行驶表是“订单”表。您正在RIGHT OUTER JOINLEFT OUTER JOIN之间切换。这将导致结果集中的混乱。

我修改了查询。看看是否有效。

SELECT
orders.id as order_id,
SUM(order_items.price) as total,
order_data.ext_order_id

FROM orders

INNER JOIN order_data
ON orders.id = order_data.ext_order_id

LEFT OUTER JOIN order_items
ON orders.id = order_items.order_id

LEFT OUTER JOIN products
ON order_items.product_id = products.product_id
WHERE order_items.product_type = 'ticket' AND products.date BETWEEN '2020-03-12' AND '2020-03-18'

GROUP BY orders.id

答案 1 :(得分:0)

我根本不明白为什么您会想要外部联接。如果我正确遵守条件:

SELECT o.id as order_id,
       SUM(oi.price) as total,
       od.ext_order_id
FROM orders o INNER JOIN
     order_data od
     ON o.id = od.ext_order_id INNER JOIN
     order_items oi
     ON o.id = oi.order_id INNER JOIN
     products p
     ON oi.product_id = p.product_id
WHERE oi.product_type = 'ticket' AND
      p.date >= '2020-03-12' AND 
      p.date < '2020-03-19'
GROUP BY o.id, od.ext_order_id;

请注意使用表别名,以便查询更易于编写和读取。

相关问题