mysql group by组内排序
来源:互联网 发布:软件测试员工资怎么样 编辑:程序博客网 时间:2024/06/17 22:34
有数据表 comments
------------------------------------------------
| id | newsID | comment | theTime |
------------------------------------------------
| 1 | 1 | aaa | 11 |
------------------------------------------------
| 2 | 1 | bbb | 12 |
------------------------------------------------
| 3 | 2 | ccc | 12 |
selet tt.id,tt.newsID,tt.comment,tt.theTime from(
select id,newsID,comment,theTime from comments order by theTime desc) as tt group by newsID
(2)
select id,newsID,comment,theTime from comments as tt group by id,newsID,comment,theTime having
------------------------------------------------
| id | newsID | comment | theTime |
------------------------------------------------
| 1 | 1 | aaa | 11 |
------------------------------------------------
| 2 | 1 | bbb | 12 |
------------------------------------------------
| 3 | 2 | ccc | 12 |
------------------------------------------------
newsID是新闻ID,每条新闻有多条评论comment,theTime是发表评论的时间
现在想要查看每条新闻的最新一条评论:
select * from comments group by newsID 显然不行
下面有两种方法可以实现:
(1)selet tt.id,tt.newsID,tt.comment,tt.theTime from(
select id,newsID,comment,theTime from comments order by theTime desc) as tt group by newsID
(2)
select id,newsID,comment,theTime from comments as tt group by id,newsID,comment,theTime having
theTime=(select max(theTime) from comments where newsID=tt.newsID)
(3)利用group_concat(comment order bytheTime desc)的组内排序功能,不过要截取字符串
原文链接:mysql group by组内排序
阅读全文
0 0
- MySQL 组内排序(Group By)
- mysql group by 组内排序
- mysql group by组内排序
- mysql group by 组内排序
- mysql group by 组内排序
- mysql group by 组内排序方法
- mysql group by 组内排序
- mysql group by组内排序
- Group by 分组后排序,组内过滤
- mysql group by排序问题
- mysql group by排序问题
- Mysql group by 排序问题
- Mysql group by 排序问题
- mysql group by排序问题
- mysql group by内排序的实现以及having和where 条件语句查询的区别
- mysql order by内排序
- 【Mysql】group by中的排序问题
- Mysql 利用group by 分组排序
- C++面向对象编程原则及人类示例
- Invalid VCS root mapping The directory C:\Users\Administrator\Desktop\github\likequanmintv-master
- Eclipse中10个最有用的快捷键组合
- XMLHttpRequest页面无刷新上传文件
- 探究Java虚拟机栈
- mysql group by组内排序
- web端跨页面传参
- Volley: [1141] BasicNetwork.performRequest: Unexpected response code 429 for https://xxxxxx.com/api/
- TaobaoSchedule调度管理控制台
- LeetCode 76. Minimum Window Substring
- Preempt Rt例程
- 第四周-单链表的的建立
- Windows下的Jupyter Notebook 安装与自定义启动
- 解决Android中No resource found that matches android:TextAppearance.Material.Widget.Button.Inverse问题