本文介紹的是MySQL中3個函數的使用,主要是針對字符串的鏈接合併處理:mysql
concatsql
concat_wsexpress
group_concatsession
<!--MORE-->ide
concat()函數是將多個字符串組合在一塊兒,造成一個大的字符串;若是鏈接的字符串中存在一個爲NULL,則輸出的結果爲NULL,語法格式爲:函數
1url
concat(str1,str2,....strn).net
3個例子????說明具體使用,如下面這個表中的第一條記錄爲例:3d
1blog
-- 一、字符之間不加鏈接符
2
mysql> select concat("01","趙雷","男");
3
+-----------------------------+
4
| concat("01","趙雷","男") |
5
+-----------------------------+
6
| 01趙雷男 |
7
+-----------------------------+
8
1 row in set (0.00 sec)
9
10
-- 二、字符之間添加鏈接符
11
mysql> select concat("01-","趙雷-","男");
12
+-------------------------------+
13
| concat("01-","趙雷-","男") |
14
+-------------------------------+
15
| 01-趙雷-男 |
16
+-------------------------------+
17
1 row in set (0.00 sec)
18
19
-- 三、忽略空字符串
20
mysql> mysql> select concat("01","趙雷","","男");
21
+--------------------------------+
22
| concat("01","趙雷","","男") |
23
+--------------------------------+
24
| 01趙雷男 |
25
+--------------------------------+
26
1 row in set (0.00 sec)
27
28
29
-- 四、存在NULL的狀況
30
mysql> select concat("01","趙雷",NULL,"男"); -- 結果直接顯示爲NULL
31
+----------------------------------+
32
| concat("01","趙雷",NULL,"男") |
33
+----------------------------------+
34
| NULL |
35
+----------------------------------+
36
1 row in set (0.01 sec)
上面的NULL是MySQL中NULL,若是NULL自己就是字符串,則結果不相同:
1
mysql> select concat("01","趙雷","NULL","男");
2
+------------------------------------+
3
| concat("01","趙雷","NULL","男") |
4
+------------------------------------+
5
| 01趙雷NULL男 |
6
+------------------------------------+
7
1 row in set (0.01 sec)
注意兩種狀況的不一樣:
concat_ws()函數相比較於concat()多了一個指定的鏈接符號,語法爲:
1
concat_ws(separator, str1, str2, str3)
第一個參數是鏈接的符號
後面的參數是待鏈接的字符
鏈接符要放在待鏈接的字符之間;分隔符也能夠是一個字符串,也能夠是其餘的參數,須要注意的是:
若是分隔符是NULL,結果爲NULL
函數後忽略任何分割符參數後的NULL值(分隔符以後的NULL值):鏈接的時候跳過NULL值
concat_ws不會忽略空字符串;concat會忽略空字符串
下面經過幾個例子來講明使用方法:
1
-- 一、指定不一樣的鏈接符號:分別指定逗號和加號
2
3
mysql> select concat_ws(",","01","趙雷","男");
4
+------------------------------------+
5
| concat_ws(",","01","趙雷","男") |
6
+------------------------------------+
7
| 01,趙雷,男 |
8
+------------------------------------+
9
1 row in set (0.00 sec)
10
11
mysql> select concat_ws("+","01","趙雷","男");
12
+------------------------------------+
13
| concat_ws("+","01","趙雷","男") |
14
+------------------------------------+
15
| 01+趙雷+男 |
16
+------------------------------------+
17
1 row in set (0.00 sec)
18
19
-- 二、不忽略空字符串
20
mysql> select concat_ws("+","01","趙雷","","男");
21
+---------------------------------------+
22
| concat_ws("+","01","趙雷","","男") |
23
+---------------------------------------+
24
| 01+趙雷++男 |
25
+---------------------------------------+
26
1 row in set (0.00 sec)
27
28
-- 三、忽略NULL;無論幾個NULL都會忽略
29
mysql> select concat_ws("+","01","趙雷",NULL,"男");
30
+-----------------------------------------+
31
| concat_ws("+","01","趙雷",NULL,"男") |
32
+-----------------------------------------+
33
| 01+趙雷+男 |
34
+-----------------------------------------+
35
1 row in set (0.00 sec)
36
37
-- 忽略兩個NULL
38
mysql> select concat_ws("+","01",NULL,"趙雷",NULL,"男");
39
+----------------------------------------------+
40
| concat_ws("+","01",NULL,"趙雷",NULL,"男") |
41
+----------------------------------------------+
42
| 01+趙雷+男 |
43
+----------------------------------------------+
44
1 row in set (0.00 sec)
group:分組的意思;concat:鏈接。合起來就是分組鏈接,具體語法爲:
1
GROUP_CONCAT(DISTINCT expression ORDER BY expression SEPARATOR sep);
DISTINCT子句用於在鏈接分組以前消除組中的重複值
ORDER BY 鏈接以前按升序或者降序排列。默認是升序
SEPARATOR指定在組中的值之間插入的文字值。若是不指定分隔符,則GROUP_CONCAT函數使用逗號(,)做爲默認分隔符
函數會自動忽略NULL值,若是全部的參數都是NULL,則結果返回NULL
GROUP_CONCAT函數返回二進制或非二進制字符串,取決於參數。 默認狀況下,返回字符串的最大長度爲1024。經過在SESSION或GLOBAL級別設置group_concat_max_len系統變量來擴展最大長度。
1
set session group_concat_max_len=18783847439738273; -- 防止超出範圍數據被截掉
下面經過這張成績表Score來說解:
1
-- 一、將每一個學生的成績單獨列出來
2
mysql> select s_id, group_concat(s_score) from Score group by s_id;
3
+------+-----------------------+
4
| s_id | group_concat(s_score) |
5
+------+-----------------------+
6
| 01 | 80,90,96 |
7
| 02 | 70,60,80 |
8
| 03 | 80,81,85 |
9
| 04 | 50,40,30 |
10
| 05 | 76,87 |
11
| 06 | 43,56 |
12
| 07 | 89,94 |
13
+------+-----------------------+
14
7 rows in set (0.01 sec)
15
16
-- 二、指定鏈接符+
17
mysql> select s_id, group_concat(s_score separator "+") from Score group by s_id;
18
+------+-------------------------------------+
19
| s_id | group_concat(s_score separator "+") |
20
+------+-------------------------------------+
21
| 01 | 80+90+96 |
22
| 02 | 70+60+80 |
23
| 03 | 80+81+85 |
24
| 04 | 50+40+30 |
25
| 05 | 76+87 |
26
| 06 | 43+56 |
27
| 07 | 89+94 |
28
+------+-------------------------------------+
29
7 rows in set (0.00 sec)
30
31
-- 三、指定排序的字段
32
-- 分數s_score已經完成了排序(指定了降序);上面的結果不指定則默認是降序
33
mysql> select s_id, group_concat(distinct s_score order by s_score desc separator "+") from Score group by s_id;
34
+------+--------------------------------------------------------------------+
35
| s_id | group_concat(distinct s_score order by s_score desc separator "+") |
36
+------+--------------------------------------------------------------------+
37
| 01 | 96+90+80 |
38
| 02 | 80+70+60 |
39
| 03 | 85+81+80 |
40
| 04 | 50+40+30 |
41
| 05 | 87+76 |
42
| 06 | 56+43 |
43
| 07 | 94+89 |
44
+------+--------------------------------------------------------------------+
45
7 rows in set (0.00 sec)
46
47
48
-- 四、去重操做
49
-- distinct s_score表示對分數去重,取出每一個學生的不一樣分數(表中每一個學生的分數都不相同,結果同上)
50
mysql> select s_id, group_concat(distinct s_score order by s_score desc separator "+") from Score group by s_id;
51
+------+--------------------------------------------------------------------+
52
| s_id | group_concat(distinct s_score order by s_score desc separator "+") |
53
+------+--------------------------------------------------------------------+
54
| 01 | 96+90+80 |
55
| 02 | 80+70+60 |
56
| 03 | 85+81+80 |
57
| 04 | 50+40+30 |
58
| 05 | 87+76 |
59
| 06 | 56+43 |
60
| 07 | 94+89 |
61
+------+--------------------------------------------------------------------+
62
7 rows in set (0.00 sec)
distinct 和order by 後面的字段是相同的