Home > Database > Mysql Tutorial > mysql多个字段拼接_MySQL

mysql多个字段拼接_MySQL

WBOY
Release: 2016-06-01 13:02:00
Original
1415 people have browsed it

Mysql的查询结果行字段拼接,可以用下面两个函数实现:

1. concat函数

mysql> select concat('1','2','3') from test ;
+---------------------+
| concat('1','2','3') |
+---------------------+
| 123 |
+---------------------+
Copy after login
如果连接串中存在NULL,则返回结果为NULL:
mysql> select concat('1','2',NULL,'3') from test ;
+--------------------------+
| concat('1','2',NULL,'3') |
+--------------------------+
| NULL |
+--------------------------+
Copy after login
2. concat_ws函数

concat(separator,str1,str2,...) 代表 concat with separator ,是concat()的特殊形式。第一个参数是其它参数的分隔符。分隔符的位置放在要连接的两个字符串之间。分隔符可以是一个字符串,也可以是其它参数。

mysql> select concat_ws(':','1','2','3') from test ;
+----------------------------+
| concat_ws(':','1','2','3') |
+----------------------------+
| 1:2:3 |
+----------------------------+
Copy after login
分隔符为NULL,则返回结果为NULL:
mysql> select concat_ws(NULL,'1','2','3') from test; 
+-----------------------------+
| concat_ws(NULL,'1','2','3') |
+-----------------------------+
| NULL | 
+-----------------------------+
Copy after login
如果参数中存在NULL,则会被忽略:
mysql> select concat_ws(':','1','2',NULL,NULL,NULL,'3') from test ;
+-------------------------------------------+
| concat_ws(':','1','2',NULL,NULL,NULL,'3') |
+-------------------------------------------+
| 1:2:3 |
+-------------------------------------------+
Copy after login
可以对NULL进行判断,并用其它值进行替
mysql>  select concat_ws(':','1','2',ifNULL(NULL,'0'),'3') from bank limit 1;  
+---------------------------------------------+
| concat_ws(':','1','2',ifNULL(NULL,'0'),'3') |
+---------------------------------------------+
| 1:2:0:3                                     | 
+---------------------------------------------+
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template