Mysql 从表中获取所有行-使用另一个表中的最新行,使用基于最新行的另一个表
我需要从orders表中获取所有详细信息,在orders status表中使用最新状态ID,然后从states表中获取该状态的名称 订单Mysql 从表中获取所有行-使用另一个表中的最新行,使用基于最新行的另一个表,mysql,sql,join,Mysql,Sql,Join,我需要从orders表中获取所有详细信息,在orders status表中使用最新状态ID,然后从states表中获取该状态的名称 订单 id | customer | product ----------------------- 1 | David | Cardboard Box id | order | status | updated_at -------------------------------- 1 | 1 | 1 | 2017-05-30 00:
id | customer | product
-----------------------
1 | David | Cardboard Box
id | order | status | updated_at
--------------------------------
1 | 1 | 1 | 2017-05-30 00:00:00
2 | 1 | 3 | 2017-05-28 00:00:00
3 | 1 | 4 | 2017-05-29 00:00:00
4 | 1 | 2 | 2017-05-26 00:00:00
5 | 1 | 5 | 2017-05-05 00:00:00
id | name
---------
1 | Pending
2 | Paid
3 | Shipped
4 | Refunded
订单到订单状态
id | customer | product
-----------------------
1 | David | Cardboard Box
id | order | status | updated_at
--------------------------------
1 | 1 | 1 | 2017-05-30 00:00:00
2 | 1 | 3 | 2017-05-28 00:00:00
3 | 1 | 4 | 2017-05-29 00:00:00
4 | 1 | 2 | 2017-05-26 00:00:00
5 | 1 | 5 | 2017-05-05 00:00:00
id | name
---------
1 | Pending
2 | Paid
3 | Shipped
4 | Refunded
订单状态
id | customer | product
-----------------------
1 | David | Cardboard Box
id | order | status | updated_at
--------------------------------
1 | 1 | 1 | 2017-05-30 00:00:00
2 | 1 | 3 | 2017-05-28 00:00:00
3 | 1 | 4 | 2017-05-29 00:00:00
4 | 1 | 2 | 2017-05-26 00:00:00
5 | 1 | 5 | 2017-05-05 00:00:00
id | name
---------
1 | Pending
2 | Paid
3 | Shipped
4 | Refunded
在本例中,我需要获得客户
和产品
,以及订单状态表中的最新状态ID,然后是该状态的名称
我该怎么做呢?它可能有一些输入错误,但查询的想法应该是这样的:
select orders.id, orders.customer, orders.product,
order_to_status.status, staus.name
from orders, order_to_status, status
where orders.id = order_to_status.order
and order_to_status.status = status.id
and order_to_status.updated_at in (
SELECT MAX(order_to_status.updated_at)
FROM order_to_status
where order_to_status.order = orders.id
group by order_to_status.order
);
select orders.id, orders.customer, orders.product,
order_to_status.status, staus.name
from orders
JOIN order_to_status ON orders.id = order_to_status.order
JOIN status ON order_to_status.status = status.id
where
order_to_status.updated_at in (
SELECT MAX(order_to_status.updated_at)
FROM order_to_status
where order_to_status.order = orders.id
group by order_to_status.order
);
我通常不使用联接,但对于联接,应如下所示:
select orders.id, orders.customer, orders.product,
order_to_status.status, staus.name
from orders, order_to_status, status
where orders.id = order_to_status.order
and order_to_status.status = status.id
and order_to_status.updated_at in (
SELECT MAX(order_to_status.updated_at)
FROM order_to_status
where order_to_status.order = orders.id
group by order_to_status.order
);
select orders.id, orders.customer, orders.product,
order_to_status.status, staus.name
from orders
JOIN order_to_status ON orders.id = order_to_status.order
JOIN status ON order_to_status.status = status.id
where
order_to_status.updated_at in (
SELECT MAX(order_to_status.updated_at)
FROM order_to_status
where order_to_status.order = orders.id
group by order_to_status.order
);
注:我添加了一个我错过的组
编辑2
我在子查询条件中出错。
更改为where order\u to\u status.order=orders.id
在where子句之后,也将组移动了。我会先将每个
订单的max(updated_at)
进行分解,然后处理您需要的所有其他内容。您可以使用子查询获取每个订单的最大日期:
select
s.`order`,
s.`status`,
s.updated_at
from order_to_statuses s
inner join
(
select
`order`,
max(updated_at) as updated_at
from order_to_statuses
group by `order`
) m
on s.`order` = m.`order`
and s.updated_at = m.updated_at
一旦你得到这个,你现在有了订单、状态id和最近的日期。使用此选项,您可以连接到其他表,进行完整查询:
select
o.customer,
o.product,
ots.updated_at,
os.name
from orders o
inner join
(
select
s.`order`,
s.`status`,
s.updated_at
from order_to_statuses s
inner join
(
select
`order`,
max(updated_at) as updated_at
from order_to_statuses
group by `order`
) m
on s.`order` = m.`order`
and s.updated_at = m.updated_at
) ots
on o.Id = ots.`order`
inner join order_states os
on ots.`status` = os.id;
请参见a使用该样本数据,预期结果是什么?(请注意,order是一个保留字,所以表/列标识符的选择很差。@所有对列名的引用要么用``包装,要么与它们的表名一起使用。@RyanHipkiss-Howorder\u表示与另一个表相关的表如何?请向我们显示预期结果,如jarlh所说,如何使用联接而不是多个From来执行此操作?这将为每个状态返回一行,而不是对它们进行分组。我已更正子查询条件中的错误,并将状态描述添加到结果中。我运行了这个查询,似乎工作正常。如果您对答案投了反对票,请查看。查询的第一部分是返回所有状态?@RyanHipkiss查询的哪一部分?@RyanHipkiss我在演示中没有看到-每个订单一个状态和日期。您的数据与您提供的数据有何不同?如果您的数据与您提供的样本不同,那么您应该包含一个更大的样本进行测试。我很快会试试第二部分。是的。回答得不错。得到我的选票——即使OP无法让自己欣赏这一努力。