mysql查询某些数据的最小值


查询一些用户的首笔订单:

 select a.* from tb_order as a right join 
(select user_id, min(create_time) as maxtime from tb_order where user_id in(111,222,333) group by user_id) as b 
on a.user_id=b.user_id and a.create_time = b.maxtime;

用如下验证,和上述sql结果匹配

select * FROM tb_order where user_id in (111) order by create_time asc limit(1);

select * FROM tb_order where user_id in (222) order by create_time asc limit(1);

select * FROM tb_order where user_id in (333) order by create_time asc limit(1);


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM