-------------已经到底啦!-------------
文中使用的例子均在数据库表db1下执行
concat()函数
功能:将多个字符串连接成一个字符串(任何一个参数为null,则返回值为null)
- 举例
1
2
3
4
5
6
7
8
9
10
11
12mysql> select concat(id,name,score)as info from db1;
+------+------+
| info |
+------+------+
|1小明0|
|2小王0|
|3小王0|
|4小李0|
|NULL |
|6小紫0|
+------+------+
6 rows in set (0.00 sec)
第5行为NULL是因为db1表中有一行的score值为null
- 分隔符
1
2
3
4
5
6
7
8
9
10
11
12mysql> select concat(id,',',name,',',score)as info from db1;
+------+------+
| info |
+------+------+
|1,小明,0|
|2,小王,0|
|3,小王,0|
|4,小李,0|
|NULL |
|6,小紫,0|
+------+------+
6 rows in set (0.00 sec)
虽然分隔符是放上去了,但却是一个一个加上去的,着实有些麻烦。有什么更加简便的方法呢?
concat_ws()函数
功能:和concat()一样,将多个字符串连接成一个字符串;==但是可以一次性指定分隔符==
- 举例
1
2
3
4
5
6
7
8
9
10
11
12mysql> select concat_ws(',',id,name,score)as info from db1;
+------+------+
| info |
+------+------+
|1,小明,0|
|2,小王,0|
|3,小王,0|
|4,小李,0|
|NULL |
|6,小紫,0|
+------+------+
6 rows in set (0.00 sec)
group_concat()函数
先理解一下group by,实际上就是分类汇总,把相同的归为一类。
关于group by的使用可以参考下篇博文:
浅析SQL中Group By的使用
- 举例
1
2
3
4
5
6
7
8
9
10
11
12mysql> select name,min(id) from db1 group by name;
+------+------+---+
| name | min(id) |
+------+------+---+
| | 7|
|krytios | 8|
|lxscloud| 9|
| 小李 | 3|
|gundum | 2|
| 小紫 | 1|
+------+------+---+
6 rows in set (0.00 sec)
下面我来解释一下这段是什么意思:
首先看select name,min(id) from db1,就是查询db1库里name和最小id的所有数据;
其次group by name就是把name相同的的人中最小的id列出。
查询name相同的人的所有的id呢?
1
2
3
4
5
6
7
8
9
10
11
12mysql> select name,min(id) from db1 order by name;
+------+------+---+
| name | min(id) |
+------+------+---+
| | 7|
|kyritios| 8|
|lxscloud| 9|
|kyritios| 3|
|lxscloud| 2|
| 小紫 | 1|
+------+------+---+
6 rows in set (0.00 sec)更进一步的
1
2
3
4
5
6
7
8
9
10
11
12mysql> select name,group_concat(id) from db1 group by name;
+------+------+------+
| name | group_concat(id) |
+------+------+------+
| | 7 |
|kyritios| 8 |
|lxscloud| 9 |
|小王 | 9,8 |
|小明 | 9,2,6|
| 小紫 | 1,9,2|
+------+------+------+
6 rows in set (0.00 sec)使用group_concat()和group by显示相同名字的人的id号
若是想要id号排序呢?
走着!!
1
2
3
4
5
6
7
8
9
10
11
12mysql> select name,group_concat(id order by id desc separator '_') from db1 group by name;
+------+------+------++------+------++------+------++------+------+
| name | group_concat(id order by id desc separator '_') |
+------+------+------++------+------++------+------++------+------+
| | 7 |
|kyritios| 8 |
|lxscloud| 9 |
|小王 | 3 |
|小明 | 2_5_6 |
| 小紫 | 1_7 |
+------+------+------++------+------++------+------++------+------+
6 rows in set (0.00 sec)还没完
1
2
3
4
5
6
7
8
9
10
11
12mysql> select name,group_concat( concat_ws('-',id,score)order by id ) from db1 group by name;
+------+------+------++------+------++------+------++------+------+
| name | group_concat( concat_ws('-',id,score)order by id ) |
+------+------+------++------+------++------+------++------+------+
| | 7-0 |
|kyritios| 8-80 |
|lxscloud| 9-0 |
|小王 | 3-0 |
|小明 | 2-0,5-0,6-0 |
| 小紫 | 1-0,7-0 |
+------+------+------++------+------++------+------++------+------+
6 rows in set (0.00 sec)以id分组,把去除重复后的name字段的值打印在一行,逗号分隔
1
2
3
4
5
6
7
8
9mysql> select id,group_concat(distinct name) from aa group by id;
+------+-----------------------------+
| id| group_concat(distinct name) |
+------+-----------------------------+
|1 | 10,20|
|2 | 20 |
|3 | 200,500 |
+------+-----------------------------+
3 rows in set (0.00 sec)==DISTINCT子句用于在连接分组之前消除组中的重复值==
以id分组,把name字段的值打印在一行,逗号分隔,以name排倒序
1
2
3
4
5
6
7
8
9mysql> select id,group_concat(name order by name desc) from aa group by id;
+------+---------------------------------------+
| id| group_concat(name order by name desc) |
+------+---------------------------------------+
|1 | 20,20,10 |
|2 | 20|
|3 | 500,200|
+------+---------------------------------------+
3 rows in set (0.00 sec)
==order by如果要按降序对值进行排序,则需要明确指定DESC选项==