[英]How to get the latest records by id Mysql?
I am using this query this returns all records associated to id_order
but i want the latest records for id_order (Adminer 4.7.5 4.7.7)我正在使用此查询,它返回与 id_order 关联的所有记录,但我想要id_order
的最新记录(Adminer 4.7.5 4.7.7)
SELECT *
FROM `order_history`
WHERE `id_order` IN (5499,5500) ORDER BY id_order_history DESC
THE DESIRED RECORDS WOULD LOOK LIKE所需的记录看起来像
Any thoughts?有什么想法吗?
You could get the most recent record for each order using a correlated query as您可以使用相关查询获取每个订单的最新记录
select *
from `order_history` o
where `id_order` in (5499,5500)
and date_add= (
select max(date_add)
from order_history
where o.id_order = id_order
)
order by id_order_history desc
If you want to pick a recent row based on maximum value of id_order_history
如果您想根据id_order_history
的最大值选择最近的一行
select *
from `order_history` o
where `id_order_history` in (5499,5500)
and id_order_history = (
select max(id_order_history )
from order_history
where o.id_order = id_order
)
order by id_order_history desc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.