MySQL 字符串拼接


在Mysql 數據庫中存在兩種字符串連接操作.具體操作如下

一. 語法:

   1. CONCAT(string1,string2,…)   說明 : string1,string2代表字符串,concat函數在連接字符串的時候,只要其中一個是NULL,那么將返回NULL

   例1:

   

   例2:

   

  2. CONCAT_WS(separator,str1,str2,...)

     說明 : string1,string2代表字符串,concat_ws 代表 concat with separator,第一個參數是其它參數的分隔符。分隔符的位置放在要連接的兩個字符串之間。分隔符可以是一個字符串,也可以是其它參數。如果分隔符為 NULL,則結果為 NULL。函數會忽略任何分隔符參數后的 NULL 值。

     舉例1:

     mysql> select concat_ws('#','dbdh=','NorthEastTrcoon',null) AS dbdh_name_three;
+-----------------------+
| dbdh_name_three       |
+-----------------------+
| dbdh=#NorthEastTrcoon |
+-----------------------+
1 row in set (0.00 sec)

    例2:

  mysql> select concat_ws(null,'dbdh=','NorthEastTrcoon',null) AS dbdh_name_fourth
;
+------------------+
| dbdh_name_fourth |
+------------------+
| NULL             |
+------------------+
1 row in set (0.00 sec)

    例3:

       mysql> select concat_ws('*','dbdh=','NorthEastTrcoon',null) AS dbdh_name_fifth;
+-----------------------+
| dbdh_name_fifth       |
+-----------------------+
| dbdh=*NorthEastTrcoon |
+-----------------------+
1 row in set (0.00 sec)

  

3. MySQL中group_concat函數
完整的語法如下:
group_concat([DISTINCT] 要連接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符'])

基本查詢

mysql> select * from stu1;
+------+------+
| id| name |
+------+------+
|1 | 10|
|1 | 20|
|1 | 20|
|2 | 20|
|3 | 200   |
|3 | 500   |
+------+------+
6 rows in set (0.00 sec)

以id分組,把name字段的值打印在一行,逗號分隔(默認)

mysql> select id,group_concat(name) from aa group by id;
+------+--------------------+
| id| group_concat(name) |
+------+--------------------+
|1 | 10,20,20|
|2 | 20 |
|3 | 200,500|
+------+--------------------+
3 rows in set (0.00 sec)

以id分組,把name字段的值打印在一行,分號分隔

mysql> select id,group_concat(name separator ';') from aa group by id;
+------+----------------------------------+
| id| group_concat(name separator ';') |
+------+----------------------------------+
|1 | 10;20;20 |
|2 | 20|
|3 | 200;500   |
+------+----------------------------------+
3 rows in set (0.00 sec)

以id分組,把去冗余的name字段的值打印在一行,

逗號分隔

mysql> select id,group_concat(distinct name) from aa group by id;
+------+-----------------------------+
| id| group_concat(distinct name) |
+------+-----------------------------+
|1 | 10,20|
|2 | 20   |
|3 | 200,500 |
+------+-----------------------------+
3 rows in set (0.00 sec)

以id分組,把name字段的值打印在一行,逗號分隔,以name排倒序

mysql> select id,group_concat(name order by name desc) from aa group by id;
+------+---------------------------------------+
| id| group_concat(name order by name desc) |
+------+---------------------------------------+
|1 | 20,20,10   |
|2 | 20|
|3 | 500,200|
+------+---------------------------------------+
3 rows in set (0.00 sec)

 

還有一個簡單的連接方式為: ||

 

mysql> select *  from student;
+----+------+-------+----------+------------+
| id | age  | score | name     | birth      |
+----+------+-------+----------+------------+
|  1 |   23 |    78 | 李四     | 2017-10-10 |
|  2 |   24 |    78 | zhangsan | 2017-10-10 |
|  3 |   25 |    99 | 王五     | 2016-05-17 |
+----+------+-------+----------+------------+
3 rows in set (0.00 sec)

mysql> select id+999,name,name+99,name+'999' from student;
+--------+----------+---------+------------+
| id+999 | name     | name+99 | name+'999' |
+--------+----------+---------+------------+
|   1000 | 李四     |      99 |        999 |
|   1001 | zhangsan |      99 |        999 |
|   1002 | 王五     |      99 |        999 |
+--------+----------+---------+------------+
3 rows in set, 6 warnings (0.00 sec)

 

 

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM