GROUP_CONCAT()函數在mysql中可以說是連接字段了,但是我們在使用GROUP_CONCAT()函數時發現此函數并沒那么簡單了,下面我來介紹關于GROUP_CONCAT()用法.
語法,代碼如下:
- GROUP_CONCAT([DISTINCT] expr [,expr ...]
- [ORDER BY {unsigned_integer | col_name | expr}
- [ASC | DESC] [,col_name ...]]
- [SEPARATOR str_val])
1.例如,代碼如下:
- SELECT student_id, GROUP_CONCAT(courses_id) AS courses FROM student_courses WHERE student_id=2 GROUP BY student_id;
- +————+———+
- | student_id | courses |
- +————+———+
- | 2 | 3,4,5 |
- +————+———+
這就不需要用php循環了,代碼如下:
- $row = $pdo->query("SELECT student_id, GROUP_CONCAT(courses_id) AS courses FROM student_courses WHERE student_id=2 GROUP BY student_id");
- $result = explode(',', $row['courses']);
2.當然分隔符還可以自定義,默認是以“,”作為分隔符,若要改為“|||”,則使用SEPARATOR來指定,代碼如下:
- SELECT student_id, GROUP_CONCAT(courses_id SEPARATOR '|||') AS courses FROM student_courses WHERE student_id=2 GROUP BY student_id;
- +————+———+
- | student_id | courses |
- +————+———+
- | 2 | 3|||4|||5 |
- +————+———+
3.除此之外,還可以對這個組的值來進行排序再連接成字符串,例如按courses_id降序來排,代碼如下:
- SELECT student_id, GROUP_CONCAT(courses_id ORDER BY courses_id DESC) AS courses FROM student_courses WHERE student_id=2 GROUP BY student_id;
- +————+———+
- | student_id | courses |
- +————+———+
- | 2 | 5,4,3 |
- +————+———+
PHP代碼如下:
- $row = $pdo->query("SELECT student_id, GROUP_CONCAT(courses_id) AS courses FROM student_courses WHERE student_id=2 GROUP BY student_id");
- $result = explode(',', $row['courses']);
分隔符還可以自定義,默認是以“,”作為分隔符,若要改為“|||”,則使用SEPARATOR來指定.
4.需要注意的:
a.int字段的連接陷阱
當你用group_concat的時候請注意,連接起來的字段如果是int型,一定要轉換成char再拼起來,否則在你執行后(ExecuteScalar或者其它任何執行SQL返回結果的方法)返回的將不是一個逗號隔開的串,而是byte[]。
該問題當你在SQLyog等一些工具中是體現不出來的,所以很難發現,代碼如下:
select group_concat(ipaddress) from t_ip 返回逗號隔開的串
select group_concat(id) from t_ip 返回byte[]
select group_concat(CAST(id as char)) from t_dep 返回逗號隔開的串
select group_concat(Convert(id , char)) from t_dep 返回逗號隔開的串
下面演示一下這個函數,先建立一個學生選課表student_courses,并填充一些測試數據,SQL代碼如下:
- CREATE TABLE student_courses (
- student_id INT UNSIGNED NOT NULL,
- courses_id INT UNSIGNED NOT NULL,
- KEY(student_id)
- );
- INSERT INTO student_courses VALUES (1, 1), (1, 2), (2, 3), (2, 4), (2, 5);
若要查找學生ID為2所選的課程,則使用下面這條SQL,SQL代碼如下:
- mysql> SELECT student_id, courses_id FROM student_courses WHERE student_id=2; --Vevb.com
- +------------+------------+
- | student_id | courses_id |
- +------------+------------+
- | 2 | 3 |
- | 2 | 4 |
- | 2 | 5 |
- +------------+------------+
- 3 rows IN SET (0.00 sec)
輸出結果有3條記錄,說明學生ID為2的學生選了3、4、5這3門課程,放在PHP里,必須用一個循環才能取到這3條記錄,如下所示:
- foreach ($pdo->query("SELECT student_id, courses_id FROM student_courses WHERE student_id=2") as $row) {
- $result[] = $row['courses_id'];
- }
而如果采用GROUP_CONCAT()函數和GROUP BY語句就顯得非常簡單了,如下所示:
- mysql> SELECT student_id, GROUP_CONCAT(courses_id) AS courses FROM student_courses WHERE student_id=2 GROUP BY student_id;
- +------------+---------+
- | student_id | courses |
- +------------+---------+
- | 2 | 3,4,5 |
- +------------+---------+
- 1 row IN SET (0.00 sec)
這樣php里處理就簡單了,代碼如下:
- $row = $pdo->query("SELECT student_id, GROUP_CONCAT(courses_id) AS courses FROM student_courses WHERE student_id=2 GROUP BY student_id");
- $result = explode(',', $row['courses']);
- //附Cast,convert的用法:
- CAST(expr AS type), CONVERT(expr,type) , CONVERT(expr USING transcoding_name)
CAST() 和CONVERT() 函數可用來獲取一個類型的值,并產生另一個類型的值,這個類型 可以是以下值其中的一個,代碼如下:
- BINARY[(N)]
- CHAR[(N)]
- DATE
- DATETIME
- DECIMAL
- SIGNED [INTEGER]
- TIME
- UNSIGNED [INTEGER]
b.長度陷阱
用了group_concat后,select里如果使用了limit是不起作用的,用group_concat連接字段的時候是有長度限制的,并不是有多少連多少,但你可以設置一下.
使用group_concat_max_len系統變量,你可以設置允許的最大長度,程序中進行這項操作的語法如下,其中 val 是一個無符號整數:
SET [SESSION | GLOBAL] group_concat_max_len = val;
若已經設置了最大長度,則結果被截至這個最大長度,在SQLyog中執行 SET GLOBAL group_concat_max_len = 10 后,重新打開SQLyog,設置就會生效.
參數設置與限制說明
1.查看服務器中設置,代碼如下:
- mysql> show variables like '%group_concat%';
- +----------------------+-------+
- | Variable_name | Value |
- +----------------------+-------+
- | group_concat_max_len | 1024 |
- +----------------------+-------+
- 1 row in set (0.00 sec)
以上設置的值說明當前是默認長度1KB.
2.改變參數值
方法一:修改配置文件中參數,新增 group_concat_max_len = 10240
方法二:在會話中實現,全局或當前session中,代碼如下:
SET GLOBAL group_concat_max_len=10240;
SET SESSION group_concat_max_len=10240;
新聞熱點
疑難解答