mysql根据属性分组找最值

初始化测试数据

我们有如下一张 product 订单表,相关字段分别为产品名称,订单数量,订单时间。

 DDL

测试

需求:根据产品名称分组,找到订单时间最新一条记录

正确的结果集应该如下:

方法一:

1
2
3
select a.*
from product a
where a.orderTime = (select max(orderTime) from product b where b.productName = a.productName);

方法二:

这里 not exists 意思是,找出a表中不满足子查询的记录,在b表中找不到比a表的 orderTime 更大的值,也就是a的值应该是最大的。

(关于 exist 和 not exists 的用法可以这样理解,exist 后面的子查询只返回 Boolean 值,将a表中的数据与子查询逐条对比,如果子查询包含该行,那么就会返回 true 存在于结果集中)

1
2
3
select a.*
from product a
where not exists(select 1 from product b where b.productName = a.productName and b.orderTime > a.orderTime);

方法三:

1
2
3
4
5
6
7
select a.*
from product a
where exists(select count(*)
             from product b
             where b.productName = a.productName
               and b.orderTime > a.orderTime
             having count(*) = 0);

方法四:

1
2
3
4
select a.*
from product a
where 1 > (select count(*) from product where productName = a.productName and orderTime > a.orderTime)
order by a.productName;

方法五:

1
2
3
4
5
select a.*
from product a
         inner join (select productName, max(orderTime) orderTime from product group by productName) b
                    on a.productName = b.productName and a.orderTime = b.orderTime
order by a.productName;

需求:根据产品名称分组,找到表中第一次出现的记录

1
2
3
4
select a.*
from product a
where orderTime = (select orderTime from product where productName = a.productName limit 1)
order by a.productName;

 结果:

需求:根据产品名称分组,找到表中订单为最新两次的记录

方法一:

1
2
3
4
select a.*
from product a
where 2 > (select count(*) from product where productName = a.productName and orderTime > a.orderTime)
order by a.productName, a.orderTime;

方法二:

1
2
3
4
5
6
7
select a.*
from product a
where exists(select count(*)
             from product
             where productName = a.productName and orderTime > a.orderTime
             having Count(*) < 2)
order by a.productName;

结果:

 

posted @   少说点话  阅读(334)  评论(1编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
网站运行:7年51天17时24分5秒
点击右上角即可分享
微信分享提示

目录导航