[英]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:我需要输出具有以下条件的订单:
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 JOIN
和LEFT 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.