MySQL拼接函数使用介绍:
MySQL常见的拼接的函数有3种
concat concat_ws group_concat
下面简单介绍下MySQL拼接函数的concat_ws和concat的使用,
具体的使用方法可以登录MySQL shell 进行help concat ; concat concat_ws ; concat_ws group_concat 来查看帮助
一.CONCAT()函数介绍:
1.1CONCAT()函数介绍:
CONCAT()函数用于将多个字符串连接成一个字符串,但是它不能指定字符作为拼接符号进行拼接字符串
语法及使用特点:
CONCAT(str1,str2,…)
返回结果为连接参数产生的字符串。如有任何一个参数为NULL ,则返回值为 NULL。可以有一个或多个参数。
官方给出的例子:
Examples:
mysql> SELECT CONCAT(‘My‘, ‘S‘, ‘QL‘);
-> ‘MySQL‘
mysql> SELECT CONCAT(‘My‘, NULL, ‘QL‘);
-> NULL
mysql> SELECT CONCAT(14.3);
-> ‘14.3‘
[email protected] [test02]>SELECT concat(‘-‘,1,22,‘你好‘) display_name;
+--------------+
| display_name |
+--------------+
| -122你好 |
+--------------+
1 row in set (0.00 sec)
使用concat函数时拼接的字符中有一个为NULL时,字符串进行拼接后则整体变成了NULL
[email protected] [test02]>SELECT concat(‘-‘,1,22,‘你好‘,NULL) display_name;
+--------------+
| display_name |
+--------------+
| NULL |
+--------------+
1 row in set (0.00 sec)
[email protected] [test02]>SELECT concat(‘-‘,1,‘你好‘,NULL,22) display_name;
+--------------+
| display_name |
+--------------+
| NULL |
+--------------+
1 row in set (0.00 sec)
1.2 concat函数简单使用介绍:
让MySQL中test01库里面所有的表数据迁移到test02库里面
[email protected] [information_schema]>select concat("rename table test01.", table_name," to test02.",table_name,";") from tables where table_schema=‘test01‘ into outfile ‘/tmp/1.sql‘;
Query OK, 2 rows affected (0.00 sec)
[[email protected]_82_178_centos tmp]# cat /tmp/1.sql
rename table test01.student3 to test02.student3;
rename table test01.test1_event to test02.test1_event;
[email protected] [information_schema]>source /tmp/1.sql
Query OK, 0 rows affected (0.10 sec)
Query OK, 0 rows affected (0.05 sec)
[email protected] [test02]>show tables;
+------------------+
| Tables_in_test02 |
+------------------+
| student3 |
| test1_event |
+------------------+
2 rows in set (0.01 sec)
[email protected] [test02]>use test01;
Database changed
[email protected] [test01]>show tables;
Empty set (0.00 sec)
二.CONCAT_WS()函数介绍:
CONCAT_WS() 代表 CONCAT With Separator ,是CONCAT()的特殊形式。第一个参数是其它参数的分隔符。分隔符的位置放在要连接的两个字符串之间。分隔符可以是一个字符串,也可以是其它参数。如果分隔符为 NULL,则结果为 NULL。函数会忽略任何分隔符参数后的 NULL 值。但是CONCAT_WS()不会忽略任何空字符串。 (然而会忽略所有的 NULL)。
2.1 concat_ws函数可以指定字符串之间的分隔符进行字符串拼接实例演示:
指定分隔符“-”进行拼接:
[email protected] [test02]>SELECT concat_ws(‘-‘,1,22,‘你好‘) display_name;
+--------------+
| display_name |
+--------------+
| 1-22-你好 |
+--------------+
1 row in set (0.00 sec)
后面为空的时候也可以拼接:
[email protected] [test02]>SELECT concat_ws(‘-‘,1,22,‘你好‘,‘‘) display_name;
+--------------+
| display_name |
+--------------+
| 1-22-你好- |
+--------------+
1 row in set (0.00 sec)
后面为NULL的时候也可以拼接:
[email protected] [test02]>SELECT concat_ws(‘-‘,1,22,‘你好‘,NULL) display_name;
+--------------+
| display_name |
+--------------+
| 1-22-你好 |
+--------------+
1 row in set (0.00 sec)
也可以指定NULL为分隔符:
[email protected] [test02]>SELECT concat_ws(‘NULL‘,1,22,‘你好‘,NULL) display_name;
+-------------------+
| display_name |
+-------------------+
| 1NULL22NULL你好 |
+-------------------+
1 row in set (0.01 sec)
[email protected] [test02]>SELECT concat_ws(‘NULL‘,1,22,‘你好‘,‘‘) display_name;
+-----------------------+
| display_name |
+-----------------------+
| 1NULL22NULL你好NULL |
+-----------------------+
1 row in set (0.00 sec)
也可以指定空为分隔符:
[email protected] [test02]>SELECT concat_ws(‘‘,1,22,‘你好‘) display_name;
+--------------+
| display_name |
+--------------+
| 122你好 |
+--------------+
1 row in set (0.00 sec)
指定拼接的标题:
[email protected] [test02]>select * from student3;
+----+------------+---------+------+
| id | teacher_id | name | sex |
+----+------------+---------+------+
| 1 | 1 | xiaosan | 男 |
| 2 | 2 | xiaosan | 男 |
| 3 | 3 | 小曼 | 女 |
| 4 | 4 | 小话 | 女 |
+----+------------+---------+------+
4 rows in set (0.00 sec)
[email protected] [test02]>SELECT CONCAT_WS(‘_‘,id,name) AS con_ws FROM student3 LIMIT 1;
+-----------+
| con_ws |
+-----------+
| 1_xiaosan |
+-----------+
1 row in set (0.00 sec)
[email protected] [test02]>SELECT CONCAT_WS(‘_‘,id,name) FROM student3 LIMIT 1;
+------------------------+
| CONCAT_WS(‘_‘,id,name) |
+------------------------+
| 1_xiaosan |
+------------------------+
1 row in set (0.00 sec)
2.2简单应用演示:
Mysql 查询数据导出为insert 格式 文件
创建从测试表:
CREATE TABLE `student3` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`teacher_id` int(11) DEFAULT NULL,
`name` varchar(20) NOT NULL,
`sex` varchar(10) DEFAULT ‘male‘,
PRIMARY KEY (`id`),
UNIQUE KEY `teacher_id` (`teacher_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 ;
mysql -uroot -p‘jianweiwutest‘ -P3306 -Ne "use test02;select concat_ws(‘\‘‘,‘insert into student3 (id,teacher_id,name,sex) values (‘, id,‘,‘,teacher_id,‘,‘,name,‘,‘,sex,‘);‘) sql_str from student3;"
Warning: Using a password on the command line interface can be insecure.
+---------------------------------------------------------------------------------+
| insert into student3 (id,teacher_id,name,sex) values (‘1‘,‘1‘,‘xiaosan‘,‘男‘); |
| insert into student3 (id,teacher_id,name,sex) values (‘2‘,‘2‘,‘xiaosan‘,‘男‘); |
+---------------------------------------------------------------------------------+
[[email protected]_82_178_centos ~]#
[email protected] [test02]>insert into student3 (id,teacher_id,name,sex) values (‘3‘,‘3‘,‘小曼‘,‘女‘);
Query OK, 1 row affected (0.07 sec)
[email protected] [test02]>insert into student3 (id,teacher_id,name,sex) values (‘4‘,‘4‘,‘小话‘,‘女‘);
Query OK, 1 row affected (0.06 sec)
[[email protected]_82_178_centos ~]# mysql -uroot -p‘jianweiwutest‘ -P3306 -Ne "use test02;select concat_ws(‘\‘‘,‘insert into student3 (id,teacher_id,name,sex) values (‘, id,‘,‘,teacher_id,‘,‘,name,‘,‘,sex,‘);‘) sql_str from student3;"
Warning: Using a password on the command line interface can be insecure.
+---------------------------------------------------------------------------------+
| insert into student3 (id,teacher_id,name,sex) values (‘1‘,‘1‘,‘xiaosan‘,‘男‘); |
| insert into student3 (id,teacher_id,name,sex) values (‘2‘,‘2‘,‘xiaosan‘,‘男‘); |
| insert into student3 (id,teacher_id,name,sex) values (‘3‘,‘3‘,‘小曼‘,‘女‘); |
| insert into student3 (id,teacher_id,name,sex) values (‘4‘,‘4‘,‘小话‘,‘女‘); |
+---------------------------------------------------------------------------------+
[[email protected]_82_178_centos ~]#
其中-Ne是执行这个SQL语句的选项,-N代表输出SQL语句执行结果中不带第一行的字段名称,-e表示要执行SQL语句;
三.GROUP_CONCAT()函数
GROUP_CONCAT函数返回一个字符串结果,该结果由分组中的值连接组合而成。
[email protected] [test02]>SELECT id,name,sex FROM student3 WHERE id IN(‘3‘,‘4‘);
+----+--------+------+
| id | name | sex |
+----+--------+------+
| 3 | 小曼 | 女 |
| 4 | 小话 | 女 |
+----+--------+------+
2 rows in set (0.00 sec)
使用语法及特点:
GROUP_CONCAT([DISTINCT] expr [,expr ...]
[ORDER BY {unsigned_integer | col_name | formula} [ASC | DESC] [,col ...]]
[SEPARATOR str_val])
在 MySQL 中,你可以得到表达式结合体的连结值。通过使用 DISTINCT 可以排除重复值。如果希望对结果中的值进行排序,可以使用 ORDER BY 子句。
SEPARATOR 是一个字符串值,它被用于插入到结果值中。缺省为一个逗号 (","),可以通过指定 SEPARATOR "" 完全地移除这个分隔符。
可以通过变量 group_concat_max_len 设置一个最大的长度。在运行时执行的句法如下: SET [SESSION | GLOBAL] group_concat_max_len = unsigned_integer;
如果最大长度被设置,结果值被剪切到这个最大长度。如果分组的字符过长,可以对系统参数进行设置:SET @@global.group_concat_max_len=40000;
[email protected] [test02]> SELECT id,GROUP_CONCAT(name) FROM student3 WHERE id IN(‘3‘,‘4‘) GROUP BY id;
+----+--------------------+
| id | GROUP_CONCAT(name) |
+----+--------------------+
| 3 | 小曼 |
| 4 | 小话 |
+----+--------------------+
2 rows in set (0.00 sec)
[email protected] [test02]> SELECT id,GROUP_CONCAT(name,teacher_id) FROM student3 WHERE id IN(‘1‘,‘4‘) GROUP BY id;
+----+-------------------------------+
| id | GROUP_CONCAT(name,teacher_id) |
+----+-------------------------------+
| 1 | xiaosan1 |
| 4 | 小话4 |
+----+-------------------------------+
2 rows in set (0.00 sec)
[email protected] [test02]> SELECT id,GROUP_CONCAT(name,sex) FROM student3 WHERE id IN(‘1‘,‘4‘) GROUP BY id;
+----+------------------------+
| id | GROUP_CONCAT(name,sex) |
+----+------------------------+
| 1 | xiaosan男 |
| 4 | 小话女 |
+----+------------------------+
2 rows in set (0.00 sec)
[email protected] [test02]>SELECT id,GROUP_CONCAT(concat_ws(‘, ‘,id,name) ORDER BY id DESC ) FROM student3 WHERE id IN(‘1‘,‘4‘) GROUP BY id;
+----+---------------------------------------------------------+
| id | GROUP_CONCAT(concat_ws(‘, ‘,id,name) ORDER BY id DESC ) |
+----+---------------------------------------------------------+
| 1 | 1, xiaosan |
| 4 | 4, 小话 |
+----+---------------------------------------------------------+
2 rows in set (0.00 sec)
[email protected] [test02]>SELECT id,GROUP_CONCAT(concat_ws(‘.‘,id,name) ORDER BY id DESC ) FROM student3 WHERE id IN(‘1‘,‘4‘) GROUP BY id;
+----+--------------------------------------------------------+
| id | GROUP_CONCAT(concat_ws(‘.‘,id,name) ORDER BY id DESC ) |
+----+--------------------------------------------------------+
| 1 | 1.xiaosan |
| 4 | 4.小话 |
+----+--------------------------------------------------------+
2 rows in set (0.01 sec)
[email protected] [test02]>SELECT id,GROUP_CONCAT(concat_ws(‘.‘,id,name,sex) ORDER BY id DESC ) FROM student3 WHERE id IN(‘1‘,‘4‘) GROUP BY id;
+----+------------------------------------------------------------+
| id | GROUP_CONCAT(concat_ws(‘.‘,id,name,sex) ORDER BY id DESC ) |
+----+------------------------------------------------------------+
| 1 | 1.xiaosan.男 |
| 4 | 4.小话.女 |
+----+------------------------------------------------------------+
2 rows in set (0.00 sec)
[email protected] [test02]>SELECT GROUP_CONCAT(concat_ws(‘.‘,id,name,sex) ORDER BY id DESC ) FROM student3 ;
+------------------------------------------------------------+
| GROUP_CONCAT(concat_ws(‘.‘,id,name,sex) ORDER BY id DESC ) |
+------------------------------------------------------------+
| 4.小话.女,3.小曼.女,2.xiaosan.男,1.xiaosan.男 |
+------------------------------------------------------------+
1 row in set (0.00 sec)
欢迎互相交流学习
原文地址:http://blog.51cto.com/wujianwei/2320073