mysql 分组内排——group_concat
2018-01-27 17:21
393 查看
在大多数应用中,group_concat函数通常用来做行列转换。其实group_concat函数还有一个很重要的功能,就是分组内排序。
group_concat完整语法:
group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符'])
1、基本用法:
1)通常,使用GROUP_CONCAT()用来做列行转换:
mysql> selectfid,name from test;
+-----+------+
| fid | name |
+-----+------+
| 1 | a |
| 1 | b |
| 1 | b |
| 2 | A |
| 2 | Z |
+-----+------+
mysql> selectfid,group_concat(name) from test group by fid;
+-----+--------------------+
| fid |group_concat(name) |
+-----+--------------------+
| 1 | a,b,b |
| 2 | A,Z |
+-----+--------------------+
2)GROUP_CONCAT()函数是支持指定分隔符的,默认是逗号,可以使用SEPARATOR
指定;
selectfid,group_concat(name separator ';') from test group by fid;
+-----+----------------------------------+
| fid |group_concat(name separator ';') |
+-----+----------------------------------+
| 1 | a;b;b |
| 2 | A;Z |
+-----+----------------------------------+3)GROUP_CONCAT()函数是支持排序的,默认通过GROUP_CONCAT()函数把列转成行的时候顺序是不确定的,可以通过ORDER
BY指定;
selectfid,group_concat(name) from test group by fid;
+-----+--------------------+
| fid |group_concat(name) |
+-----+--------------------+
| 1 | a,b,b |
| 2 | A,Z,C |
+-----+--------------------+
mysql> selectfid,group_concat(name order by name desc) from test group by fid;
+-----+---------------------------------------+
| fid |group_concat(name order by name desc) |
+-----+---------------------------------------+
| 1 | b,b,a |
| 2 | Z,C,A |
+-----+---------------------------------------+4)GROUP_CONCAT()函数是支持去重功能的,通过distinct关键字可以实现;
mysql> selectfid,group_concat(name) from test group by fid;
+-----+--------------------+
| fid |group_concat(name) |
+-----+--------------------+
| 1 | a,b,b |
| 2 | A,Z |
+-----+--------------------+
selectfid,group_concat(distinct name) from test group by fid;
+-----+-----------------------------+
| fid |group_concat(distinct name) |
+-----+-----------------------------+
| 1 | a,b |
| 2 | A,Z |
+-----+-----------------------------+
2、分组内排序:
有数据表 comments
------------------------------------------------
| id | newsID | comment | theTime |
------------------------------------------------
| 1 | 1 | aaa | 11 |
------------------------------------------------
| 2 | 1 | bbb | 12 |
------------------------------------------------
| 3 | 2 | ccc | 12 |
------------------------------------------------newsID是新闻ID,每条新闻有多条评论comment,theTime是发表评论的时间.现在想要查看每条新闻的最新一条评论。
1)下面这两种方法显然不行:
select * from comments group by newsID这种方式,newId=1的新闻,组内评论没有进行排序,mysql会默认展示出第一个出现的评论。
select * from comments group by newsID order by theTime desc
这种方式也不行,这是组外排序。
2)使用group_concat函数实现组内排序:
select newsID,group_concat(comment,Order BY theTime DESC),theTime from comments as tt group by newsID
3)其他方法:
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 或者
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)
group_concat完整语法:
group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符'])
1、基本用法:
1)通常,使用GROUP_CONCAT()用来做列行转换:
mysql> selectfid,name from test;
+-----+------+
| fid | name |
+-----+------+
| 1 | a |
| 1 | b |
| 1 | b |
| 2 | A |
| 2 | Z |
+-----+------+
mysql> selectfid,group_concat(name) from test group by fid;
+-----+--------------------+
| fid |group_concat(name) |
+-----+--------------------+
| 1 | a,b,b |
| 2 | A,Z |
+-----+--------------------+
2)GROUP_CONCAT()函数是支持指定分隔符的,默认是逗号,可以使用SEPARATOR
指定;
selectfid,group_concat(name separator ';') from test group by fid;
+-----+----------------------------------+
| fid |group_concat(name separator ';') |
+-----+----------------------------------+
| 1 | a;b;b |
| 2 | A;Z |
+-----+----------------------------------+3)GROUP_CONCAT()函数是支持排序的,默认通过GROUP_CONCAT()函数把列转成行的时候顺序是不确定的,可以通过ORDER
BY指定;
selectfid,group_concat(name) from test group by fid;
+-----+--------------------+
| fid |group_concat(name) |
+-----+--------------------+
| 1 | a,b,b |
| 2 | A,Z,C |
+-----+--------------------+
mysql> selectfid,group_concat(name order by name desc) from test group by fid;
+-----+---------------------------------------+
| fid |group_concat(name order by name desc) |
+-----+---------------------------------------+
| 1 | b,b,a |
| 2 | Z,C,A |
+-----+---------------------------------------+4)GROUP_CONCAT()函数是支持去重功能的,通过distinct关键字可以实现;
mysql> selectfid,group_concat(name) from test group by fid;
+-----+--------------------+
| fid |group_concat(name) |
+-----+--------------------+
| 1 | a,b,b |
| 2 | A,Z |
+-----+--------------------+
selectfid,group_concat(distinct name) from test group by fid;
+-----+-----------------------------+
| fid |group_concat(distinct name) |
+-----+-----------------------------+
| 1 | a,b |
| 2 | A,Z |
+-----+-----------------------------+
2、分组内排序:
有数据表 comments
------------------------------------------------
| id | newsID | comment | theTime |
------------------------------------------------
| 1 | 1 | aaa | 11 |
------------------------------------------------
| 2 | 1 | bbb | 12 |
------------------------------------------------
| 3 | 2 | ccc | 12 |
------------------------------------------------newsID是新闻ID,每条新闻有多条评论comment,theTime是发表评论的时间.现在想要查看每条新闻的最新一条评论。
1)下面这两种方法显然不行:
select * from comments group by newsID这种方式,newId=1的新闻,组内评论没有进行排序,mysql会默认展示出第一个出现的评论。
select * from comments group by newsID order by theTime desc
这种方式也不行,这是组外排序。
2)使用group_concat函数实现组内排序:
select newsID,group_concat(comment,Order BY theTime DESC),theTime from comments as tt group by newsID
3)其他方法:
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 或者
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)
相关文章推荐
- mysql分组后group_concat()的使用场景总结
- mysql的GROUP_CONCAT(expr),分组合并字段
- mysql GROUP_CONCAT获取分组的前几名
- mysql group_concat 分组,多字段统一排序及输出数据到java
- mysql查询分组显示数据结果之GROUP_CONCAT
- mysql查询分组归类函数-group_concat,通常与group_by一起使用
- mysql分组并多行拼接--group_concat和group by的使用
- MySQL使用GROUP_CONCAT()函数按ID分组将多条记录的某个字段以逗号或者指定分隔号隔开成一条记录
- mysql分组合并GROUP_CONCAT
- mysql group_concat聚合分组
- mysql GROUP_CONCAT+ GROUP BY + substring_index获取分组的前几名
- mysql GROUP_CONCAT 可以将分组的字段进行拼接处理.
- mysql 分组查询以及显示各个分组所有的温度和时间要用GROUP_CONCAT
- mysql 分组统计(直播内容状态,带上内容id),count case when group_concat sql
- mysql 分组拼接 GROUP_CONCAT
- mysql 分组统计(直播内容状态,带上内容id),count case when group_concat sql
- mysql 查询多条结果,合并成一个结果,使用 group_concat()函数
- Mysql group_concat的反向应用实现(Mysql列转行)
- Mysql GROUP_CONCAT 将Decimal类型转化为 blob类型
- MySQL中函数CONCAT及GROUP_CONCAT