简体   繁体   English

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

[英]How do I join multiple (four) tables using sql with conditions?

I am trying to create an SQL query that conditionally pulls data from multiple tables.我正在尝试创建一个有条件地从多个表中提取数据的 SQL 查询。

I have four tables:我有四个表:

orders订单

+------+------------+------------+
| 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   |
+------+--------------+---------------+

products产品

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

I need to output orders with the following conditions:我需要输出具有以下条件的订单:

  • Each order in a row with total (calculated from order items with matching order id)一行中的每个订单与总计(根据具有匹配订单 ID 的订单项计算)
  • The 'ext_order_ref' from the order_data table that matches that order order_data 表中与该订单匹配的“ext_order_ref”
  • Only include order items that have a specific product type仅包含具有特定产品类型的订单商品
  • Only include orders with products from a particular date range仅包含具有特定日期范围内产品的订单

Preferred output would look like this:首选输出如下所示:

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

My current query is basically like this;我目前的查询基本上是这样的; please advise请指教

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

It almost works, but not quite.它几乎有效,但不完全有效。 The date particularly is causing issue.日期尤其引起问题。

Thanks in advance!提前致谢!

First decide the driving table for the query and form the query based on the driving table.首先决定查询的驱动表,并根据驱动表形成查询。 Driving table is the one primary table from which other tables join.驱动表是其他表从中加入的一个主表。

More information on driving tables from askTom 来自 askTom 的有关驾驶台的更多信息

In your case, the driving table is Orders table.在您的情况下,驱动表是订单表。 You are switching between RIGHT OUTER JOIN and LEFT OUTER JOIN .您正在RIGHT OUTER JOINLEFT OUTER JOIN之间切换。 This will cause confusion in the resultset.这将导致结果集中的混乱。

I have modified the query.我已经修改了查询。 See whether it works.看看它是否有效。

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

I don't understand why you would want outer joins at all.我不明白你为什么想要外连接。 If I follow the conditions correctly:如果我正确地遵循条件:

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;

Note the use of table aliases so the query is easier to write and read.请注意表别名的使用,以便查询更易于编写和阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM