MySQL 数据库中 substring() 字符截取和 concat() 字符拼接


1、MySQL中,substring() 是截取字符串函数

  • 使用语法为:
select substring(str,pos)
select substring(str from pos)
select substring(str,pos,len)
select substring(str from pos for len)
select substring('helloWorld', 6, 5)   # 返回结果是 "World"
  • 参数解释:
    • 第一个参数 str 为要截取的字符串
    • 第二个参数 pos 是从第几个字符开始(包括这个字符)截取  
    • 第三个参数 len 是表示想要截取的字符串的长度
    • 如果只有两个参数则表示从第 pos 个字符开始截取到最后一个字符  
    • pos 的值是可以为负数的,当 pos 为 -5 时则表示从字符串的倒数第 5 个字符开始截取
select substring('helloWorld', -5, 3)     # 结果为:"Wor"
  • 实例用法:
select substring(rand(), 3, 6)    # 随机生成一个 6 位数的字符串

2、MySQL中,concat() 是拼接字符串

  • concat(str1,str2,…) 
    • 返回结果为连接参数产生的字符串。如有任何一个参数为 NULL ,则返回值为 NULL 
  • MySQL 的 concat 函数可以连接一个或者多个字符串,如
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
mysql> select concat( '10' );
+ --------------+
| concat( '10' ) |
+ --------------+
| 10  |
+ --------------+
1 row in set (0.00 sec)
 
mysql> select concat( '11' , '22' , '33' );
+ ------------------------+
| concat( '11' , '22' , '33' ) |
+ ------------------------+
| 112233 |
+ ------------------------+
1 row in set (0.00 sec)
  • MySQL 的 concat 函数在连接字符串的时候,只要其中一个是 NULL,那么将返回 NULL
1
2
3
4
5
6
7
mysql> select concat( '11' , '22' , null );
+ ------------------------+
| concat( '11' , '22' , null ) |
+ ------------------------+
| NULL  |
+ ------------------------+
1 row in set (0.00 sec)

3、MySQL 中 concat_ws 函数

  • CONCAT_WS(separator,str1,str2,...)
  • CONCAT_WS() 代表 CONCAT With Separator ,是 CONCAT() 的特殊形式。第一个参数是其它参数的分隔符。分隔符的位置放在要连接的两个字符串之间。分隔符可以是一个字符串,也可以是其它参数。
  • 注意:
    • 如果分隔符为 NULL,则结果为 NULL。函数会忽略任何分隔符参数后的 NULL 值。  
    • 如连接后以逗号分隔  
1
2
3
4
5
6
7
8
mysql> select concat_ws( ',' , '11' , '22' , '33' );
 
+ -------------------------------+
| concat_ws( ',' , '11' , '22' , '33' ) |
+ -------------------------------+
| 11,22,33 |
+ -------------------------------+
1 row in set (0.00 sec)
  • 和 MySQL 中 concat 函数不同的是,concat_ws 函数在执行的时候,不会因为 NULL 值而返回 NULL
1
2
3
4
5
6
7
mysql> select concat_ws( ',' , '11' , '22' , NULL );
+ -------------------------------+
| concat_ws( ',' , '11' , '22' , NULL ) |
+ -------------------------------+
| 11,22 |
+ -------------------------------+
1 row in set (0.00 sec)

4、MySQL 中 group_concat 函数

  • group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符'])
1
2
3
4
5
6
7
8
9
10
11
12
mysql> select * from aa;
+ ------+------+
| id| name |
+ ------+------+
|1 | 10|
|1 | 20|
|1 | 20|
|2 | 20|
|3 | 200  |
|3 | 500  |
+ ------+------+
6 rows in set (0.00 sec)
  • 以 id 分组,把 name 字段的值打印在一行,逗号分隔(默认)
1
2
3
4
5
6
7
8
9
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 字段的值打印在一行,分号分隔
1
2
3
4
5
6
7
8
9
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字段的值打印在一行,逗号分隔
1
2
3
4
5
6
7
8
9
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 排倒序
1
2
3
4
5
6
7
8
9
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)

5、repeat()函数

  • 用来复制字符串,如下 'ab' 表示要复制的字符串,2 表示复制的份数
1
2
3
4
5
6
7
8
9
mysql> select repeat( 'ab' ,2);
 
+ ----------------+
| repeat( 'ab' ,2) |
+ ----------------+
| abab      |
+ ----------------+
 
1 row in set (0.00 sec)
1
2
3
4
5
6
7
8
mysql> select repeat( 'a' ,2);
 
+ ---------------+
| repeat( 'a' ,2) |
+ ---------------+
| aa      |
+ ---------------+
1 row in set (0.00 sec)

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM