mysql------ group by语句

https://www.cnblogs.com/hhe0/p/9556070.html

group by 语句的执行过程分两步,第一步按照group by 后面的分组字段进行分组,然后取每组的第一条记录进行组合做为结果集。

如:表

表中数据如下: 

 (1、)select * from repeat_user group by user_id结果为:

因为分组后是得到两组,user_id为1的一组:3 a c 1,5 a b 1;user_id为2的为一组:4 a c 2,6 a b 2。这个排序是因为id为主键,默认按照主键排序。所以取两组的第一条记录做为结果集。

(2、)现在希望得到时间最早的两条user_id记录(user_id不一样):
select * from (select * from repeat_user order  by create_time)a  group by user_id ;结果集为:

扫描二维码关注公众号,回复: 5808340 查看本文章

猜你喜欢

转载自blog.csdn.net/w_t_y_y/article/details/88884746