- 原始写法,先order by 再 group by
SELECT p_id,content,create_date from (
SELECT
b.id,
b.p_id,
b.content,
b.create_date
FROM
con_reply b
ORDER BY create_date desc
) t2 GROUP BY p_id
但是结果取的p_id对应的并不是最新日期的数据
- 修改后正确(把group by id 结果 order by 排序字段,当前记录的主键,group by之后这个排序的作用会保留)
SELECT p_id,content,create_date from (
SELECT
b.id,
b.p_id,
b.content,
b.create_date
FROM
con_reply b
GROUP BY id ORDER BY create_date desc
) t2 GROUP BY p_id