coding
coding喵的gravatar头像
coding喵2019-06-25 14:19:31

mysql 分组查每组里创建时间最大的那条数据

mysql 分组查每组里创建时间最大的那条数据

所有回答列表(3)
khbwgh的gravatar头像
khbwgh LV26月26日

给你提供一个思路,按时间排序给数据加一个自动增长的列 r,然后

select * rom t where r in(
select  max(r) from t group by userid
)

ls1212121的gravatar头像
ls1212121 LV17月3日

类似于这种

SELECT * FROM (SELECT * FROM posts ORDER BY dateline DESC)  BIAOMING GROUP BY  tid ORDER BY dateline DESC LIMIT 10

 

Ares_xue的gravatar头像
Ares_xue LV67月5日
#1.子查询
SELECT *
FROM student s1
WHERE student_age = (
  SELECT max(s2.student_age)
  FROM student s2
  WHERE s1.class_id = s2.class_id);
# 2.连接
SELECT *
FROM student s1
  JOIN (
         SELECT
           class_id,
           max(student_age) AS age
         FROM student
         GROUP BY class_id) AS s2
    ON s1.class_id = s2.class_id AND s1.student_age = s2.age;
#3.左连接
SELECT *
FROM student s1
  LEFT JOIN student s2 ON
                         s1.class_id = s2.class_id AND s1.student_age < s2.student_age
WHERE s2.class_id IS NULL;
顶部客服微信二维码底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友