Mysql的查询结果行字段拼接,能够用以下两个函数实现:
1. concat函数
mysql> select concat('1','2','3') from test ; +---------------------+ | concat('1','2','3') | +---------------------+ | 123 | +---------------------+假设连接串中存在NULL,则返回结果为NULL:
mysql> select concat('1','2',NULL,'3') from test ; +--------------------------+ | concat('1','2',NULL,'3') | +--------------------------+ | NULL | +--------------------------+
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 | +----------------------------+分隔符为NULL,则返回结果为NULL:
mysql> select concat_ws(NULL,'1','2','3') from test; +-----------------------------+ | concat_ws(NULL,'1','2','3') | +-----------------------------+ | NULL | +-----------------------------+
mysql> select concat_ws(':','1','2',NULL,NULL,NULL,'3') from test ; +-------------------------------------------+ | concat_ws(':','1','2',NULL,NULL,NULL,'3') | +-------------------------------------------+ | 1:2:3 | +-------------------------------------------+
能够对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 | +---------------------------------------------+