mysql多个字段拼接

5-05 2,908 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开启表锁备份数据

1、全局读锁定: FLUSH TABLES WITH READ LOCK ; 执行了命令之后所有库所有表都被锁定只读,一般用在数据库联机备份,这个时候数据库的写操作将被阻塞,读操...

阅读全文

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

阅读全文

欢迎留言