mysql多个字段拼接

5-05 1,464 views

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函数"

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 |   
+-----------------------------+  

如果参数中存在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                                     |   
+---------------------------------------------+  

Mysql事务的四种隔离级别(转)

转载地址(click me) 数据库事务的隔离级别有4种,由低到高分别为Read uncommitted 、Read committed 、Repeatable read 、Serializable 。而且,在事务的并...

阅读全文

centos yum安装mysql5.7 或 mysql8.0

参考:http://www.koukousky.com/back/1296.html 参考:https://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/ $ wget https://dev.mysql.com/get/mysql...

阅读全文

Mysql 分页优化

在系统中需要进行分页的时候,我们通常会使用limit 加上偏移量的方法实现,再加上合适的order by 子句,如果有对应的索引,通常效率会很不错,否则mysql需要...

阅读全文

欢迎留言