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