MySQL字符串处理函数的用法及使用举例
MySQL提供了处理字符串的相关函数。现对这些函数的功能及用法进行介绍并举例。
创建数据表emp并输入数据:
create table customer(
c_id int primary key auto_increment comment '客户编号',
c_name char(20) not null default '' comment '客户名称',
contact_person char(20) not null default '' comment '联系人',
phone char(20) not null default '' comment '联系电话',
addr varchar(200) not null default '' comment '客户地址'
);
insert into customer(c_name,contact_person,phone,addr)
values('新乡市大路食品公司','王江涛','13603735218','河南省新乡市'),
('武汉市晨阳印刷公司','张明远','13837842215','湖北省武汉市'),
('郑州市明星食品公司','刘芳芳','13837125588','河南省郑州市'),
('长沙市明阳花炮公司','赵珍珍','13969821158','湖南省长沙市'),
('湖北开元房地产公司','刘鹏飞','13536872256','湖北省武汉市'),
('河南明天铜业公司','王大陆','13937365288','河南省新乡市');
一、ascii()函数
ascii()函数的语法如下:
ascii(str);
说明:返回字符串中第一个字符的ascii码。
举例:
mysql> select ascii('abc') as result;
+--------+
| result |
+--------+
| 97 |
+--------+
1 row in set (0.00 sec)
mysql> select ascii('b') as result;
+--------+
| result |
+--------+
| 98 |
+--------+
1 row in set (0.00 sec)
mysql> select ascii('012') as result;
+--------+
| result |
+--------+
| 48 |
+--------+
1 row in set (0.00 sec)
二、bin()函数
bin()函数的语法如下:
bin(n);
说明:把数值n转化为二进制形式,返回值是一个由0和1组成的字符串。
举例:
mysql> select bin(58) as result;
+--------+
| result |
+--------+
| 111010 |
+--------+
1 row in set (0.00 sec)
mysql> select bin(121) as result;
+---------+
| result |
+---------+
| 1111001 |
+---------+
1 row in set (0.00 sec)
mysql> select bin(1024) as result;
+-------------+
| result |
+-------------+
| 10000000000 |
+-------------+
1 row in set (0.00 sec)
三、char()函数
char()函数的语法如下:
char(n1,n2,) ;
说明:返回由n1,n2,…的ascii码对应的字符组成的字符串。
举例:
mysql> select char(97,98,99,100) as result;
+--------+
| result |
+--------+
| abcd |
+--------+
1 row in set (0.00 sec)
mysql> select char(97,98,48,65,66) as result;
+--------+
| result |
+--------+
| ab0AB |
+--------+
1 row in set (0.00 sec)
四、char_length()函数
char_length()函数的语法如下:
char_length(str);
说明:返回字符串中包含字符的个数。半角字符和全角字符长度都为1。
举例:
mysql> select char_length('wang123') as result;
+--------+
| result |
+--------+
| 7 |
+--------+
1 row in set (0.00 sec)
mysql> select char_length('王明明abc') as result;
+--------+
| result |
+--------+
| 6 |
+--------+
1 row in set (0.00 sec)
mysql> select c_id,c_name,char_length(c_name) as c_name_length from customer;
+------+-----------------------------+---------------+
| c_id | c_name | c_name_length |
+------+-----------------------------+---------------+
| 1 | 新乡市大路食品公司 | 9 |
| 2 | 武汉市晨阳印刷公司 | 9 |
| 3 | 郑州市明星食品公司 | 9 |
| 4 | 长沙市明阳花炮公司 | 9 |
| 5 | 湖北开元房地产公司 | 9 |
| 6 | 河南明天铜业公司 | 8 |
+------+-----------------------------+---------------+
6 rows in set (0.00 sec)
五、length()函数
length()函数的语法如下:
length(str);
说明:返回字符串的长度(用字节表示)。多字节字符的长度取决于所用的字符集。比如utf8字符集一个汉字的字节数为3,而latin1字符集一个汉字的字节数为2。
举例:
mysql> select length('wang123') as result;
+--------+
| result |
+--------+
| 7 |
+--------+
1 row in set (0.00 sec)
mysql> select length('王明明abc') as result;
+--------+
| result |
+--------+
| 12 |
+--------+
1 row in set (0.00 sec)
六、concat()函数
concat()函数的语法如下:
concat(str1,str2,...);
说明:把参数中包含的字符串连接成一个字符串。
举例:
mysql> select concat('河南省','新乡市','红旗区') as result;
+-----------------------------+
| result |
+-----------------------------+
| 河南省新乡市红旗区 |
+-----------------------------+
1 row in set (0.00 sec)
七、concat_ws()函数
concat_ws()函数的语法如下:
concat_ws(separator,st1,st2,...);
说明:把参数中包含的字符串连接成一个字符串,并且用一个分隔符隔开。
举例:
mysql> select concat_ws('|','河南省','新乡市','红旗区') as result;
+-------------------------------+
| result |
+-------------------------------+
| 河南省|新乡市|红旗区 |
+-------------------------------+
1 row in set (0.01 sec)
七、group_concat()函数
group_concat()函数的语法如下:
group_concat([distinct] fieldname [order by 排序字段 asc|desc] separator '分隔符');
说明:把表中字段的所有数据使用separator指定的分隔符连接起来,如果省略separator,则默认的分隔符为逗号。
举例:
mysql> select group_concat(c_name) from customer where addr like '河南%';
+----------------------------------------------------------------------------------+
| group_concat(c_name) |
+----------------------------------------------------------------------------------+
| 新乡市大路食品公司,郑州市明星食品公司,河南明天铜业公司 |
+----------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select group_concat(c_name separator '|') from customer where addr like '河南%';
+----------------------------------------------------------------------------------+
| group_concat(c_name separator '|') |
+----------------------------------------------------------------------------------+
| 新乡市大路食品公司|郑州市明星食品公司|河南明天铜业公司 |
+----------------------------------------------------------------------------------+
1 row in set (0.00 sec)
九、format()函数
format()函数的语法如下:
format(x,d);
说明:对数值x进行四舍五入运算,保留d为小数,返回值为一个字符串。
举例:
mysql> select format(12.5871,2) as result;
+--------+
| result |
+--------+
| 12.59 |
+--------+
1 row in set (0.00 sec)
mysql> select format(2.5215,2) as result;
+--------+
| result |
+--------+
| 2.52 |
+--------+
1 row in set (0.01 sec)
十、字符串替换函数
字符串替换有两个函数,insert()和replace()函数,语法如下:
insert(str,pos,len,instr);
replace(str1,str2,str3)
说明:
1、insert()函数:把字符串str从pos位置开始的len个字符替换为instr字符串,并返回替换之后的字符串。如果pos超过字符串长度,则不进行任何替换,直接返回原字符串。
2、replace()函数:把str1中的str2子串替换为str3子串。
举例:
mysql> select c_name,contact_person,insert(phone,4,4,'****') as phone from customer;
+-----------------------------+----------------+-------------+
| c_name | contact_person | phone |
+-----------------------------+----------------+-------------+
| 新乡市大路食品公司 | 王江涛 | 136****5218 |
| 武汉市晨阳印刷公司 | 张明远 | 138****2215 |
| 郑州市明星食品公司 | 刘芳芳 | 138****5588 |
| 长沙市明阳花炮公司 | 赵珍珍 | 139****1158 |
| 湖北开元房地产公司 | 刘鹏飞 | 135****2256 |
| 河南明天铜业公司 | 王大陆 | 139****5288 |
+-----------------------------+----------------+-------------+
6 rows in set (0.00 sec)
mysql> select c_name,insert(contact_person,2,10,'*') as contact_person,
insert(phone,4,4,'****') as phone from customer;
+-----------------------------+----------------+-------------+
| c_name | contact_person | phone |
+-----------------------------+----------------+-------------+
| 新乡市大路食品公司 | 王* | 136****5218 |
| 武汉市晨阳印刷公司 | 张* | 138****2215 |
| 郑州市明星食品公司 | 刘* | 138****5588 |
| 长沙市明阳花炮公司 | 赵* | 139****1158 |
| 湖北开元房地产公司 | 刘* | 135****2256 |
| 河南明天铜业公司 | 王* | 139****5288 |
+-----------------------------+----------------+-------------+
6 rows in set (0.00 sec)
mysql> select c_name,replace(c_name,'公司','有限公司') from customer;
+-----------------------------+-----------------------------------------+
| c_name | replace(c_name,'公司','有限公司') |
+-----------------------------+-----------------------------------------+
| 新乡市大路食品公司 | 新乡市大路食品有限公司 |
| 武汉市晨阳印刷公司 | 武汉市晨阳印刷有限公司 |
| 郑州市明星食品公司 | 郑州市明星食品有限公司 |
| 长沙市明阳花炮公司 | 长沙市明阳花炮有限公司 |
| 湖北开元房地产公司 | 湖北开元房地产有限公司 |
| 河南明天铜业公司 | 河南明天铜业有限公司 |
+-----------------------------+-----------------------------------------+
6 rows in set (0.00 sec)
十一、字符的大小写转换函数
大小写转换函数一共有四个,语法如下:
lcase(str);
lower(str);
ucase(str);
upper(str);
说明:把字符串中的字母转换为大写字母(ucase或upper)或小写字母(lcase或lower),非字母不转换。
举例:
mysql> set @str='I am a Student.@';
Query OK, 0 rows affected (0.00 sec)
mysql> select lower(@str) as result1,lcase(@str) as result2;
+------------------+------------------+
| result1 | result2 |
+------------------+------------------+
| i am a student.@ | i am a student.@ |
+------------------+------------------+
1 row in set (0.00 sec)
mysql> select upper(@str) as result1,ucase(@str) as result2;
+------------------+------------------+
| result1 | result2 |
+------------------+------------------+
| I AM A STUDENT.@ | I AM A STUDENT.@ |
+------------------+------------------+
1 row in set (0.00 sec)
十二、截取子字符串函数
截取子字符串函数有三个,语法如下:
left(str,n);
right(str,n);
substring(str,m,n);
substr(str,m,n);
说明:left()函数从字符串最前端开始截取n个字符并返回,right()函数从字符串最右端截取n个字符并返回;substr()和substring()函数从第m个字符开始,截取n个字符并返回。
举例:
mysql> set @str='河南省新乡市123456';
Query OK, 0 rows affected (0.00 sec)
mysql> select left(@str,3) as result;
+-----------+
| result |
+-----------+
| 河南省 |
+-----------+
1 row in set (0.00 sec)
mysql> select right(@str,6) as result;
+--------+
| result |
+--------+
| 123456 |
+--------+
1 row in set (0.00 sec)
mysql> select sbustr(@str,4,3) as result;
ERROR 1305 (42000): FUNCTION test.sbustr does not exist
mysql> select sbustring(@str,4,3) as result;
ERROR 1305 (42000): FUNCTION test.sbustring does not exist
mysql> select substring(@str,4,3) as result;
+-----------+
| result |
+-----------+
| 新乡市 |
+-----------+
1 row in set (0.00 sec)
mysql> select substr(@str,4,6) as result;
+--------------+
| result |
+--------------+
| 新乡市123 |
+--------------+
1 row in set (0.00 sec)
十三、查找字符串函数
查找字符串函数有两个,语法如下:
locate(str1,str2);
position(str1 in str2);
说明:如果str1是str2的子字符串,则返回子字符串第一次出现的位置,否则返回0。locate函数和position函数用法相同,唯一的不同点是locate函数的两个参数用逗号隔开,position函数的两个参数用in隔开。
举例:
mysql> select position('新乡市' in '河南省新乡市红旗区') as result;
+--------+
| result |
+--------+
| 4 |
+--------+
1 row in set (0.00 sec)
mysql> select position('新乡市' in '河南新乡红旗区') as result;
+--------+
| result |
+--------+
| 0 |
+--------+
1 row in set (0.00 sec)
mysql> select * from customer where locate('食品',c_name)>0;
+------+-----------------------------+----------------+-------------+--------------------+
| c_id | c_name | contact_person | phone | addr |
+------+-----------------------------+----------------+-------------+--------------------+
| 1 | 新乡市大路食品公司 | 王江涛 | 13603735218 | 河南省新乡市 |
| 3 | 郑州市明星食品公司 | 刘芳芳 | 13837125588 | 河南省郑州市 |
+------+-----------------------------+----------------+-------------+--------------------+
2 rows in set (0.00 sec)
mysql> select * from customer where position('明' in c_name)>0;
+------+-----------------------------+----------------+-------------+--------------------+
| c_id | c_name | contact_person | phone | addr |
+------+-----------------------------+----------------+-------------+--------------------+
| 3 | 郑州市明星食品公司 | 刘芳芳 | 13837125588 | 河南省郑州市 |
| 4 | 长沙市明阳花炮公司 | 赵珍珍 | 13969821158 | 湖南省长沙市 |
| 6 | 河南明天铜业公司 | 王大陆 | 13937365288 | 河南省新乡市 |
+------+-----------------------------+----------------+-------------+--------------------+
3 rows in set (0.00 sec)
十四、repeat()函数
repeat()函数的语法如下:
repeat(str,n);
说明:生成一个新字符串,该字符串有n个str连接而成。
举例:
mysql> select repeat('Mysql',3) as result;
+-----------------+
| result |
+-----------------+
| MysqlMysqlMysql |
+-----------------+
1 row in set (0.00 sec)
mysql> select repeat('Hello! ',3) as result;
+-----------------------+
| result |
+-----------------------+
| Hello! Hello! Hello! |
+-----------------------+
1 row in set (0.00 sec)
十五、lpad()与rpad()函数
lpad()与rpad()函数的语法如下:
lpad(str,len,padstr);
rpad(str,len,padstr);
说明:把字符串str用padstr字符串填充到长度为len,并返回长度为len的字符串。lpad函数str字符串右对齐,padstr从左边填充,rpad函数str字符串左对齐,padstr从右端填充。
举例:
mysql> select lpad('MySQL',15,'?') as result1,rpad('MySQL',15,'?');
+-----------------+----------------------+
| result1 | rpad('MySQL',15,'?') |
+-----------------+----------------------+
| ??????????MySQL | MySQL?????????? |
+-----------------+----------------------+
1 row in set (0.00 sec)
mysql> select lpad('MySQL',15,'ha') as result1,rpad('MySQL',15,'ha');
+-----------------+-----------------------+
| result1 | rpad('MySQL',15,'ha') |
+-----------------+-----------------------+
| hahahahahaMySQL | MySQLhahahahaha |
+-----------------+-----------------------+
1 row in set (0.00 sec)
十六、space()函数
space()函数的语法如下:
space(n);
说明:返回由n个空格构成的字符串。
举例:
mysql> select concat(contact_person,space(3),phone) as contact from customer;
+-------------------------+
| contact |
+-------------------------+
| 王江涛 13603735218 |
| 张明远 13837842215 |
| 刘芳芳 13837125588 |
| 赵珍珍 13969821158 |
| 刘鹏飞 13536872256 |
| 王大陆 13937365288 |
+-------------------------+
6 rows in set (0.00 sec)
十七、sustring_index()函数
sustring_index()函数的语法如下:
substring_index(str,delimiter,n);
说明:如果n大于0,返回第n个delimiter所表示字符的左边的所有内容,如果n小于0,返回第n个delimiter所表示字符的右边的所有内容。
举例:
mysql> select substring_index('www.baidu.com','.',1) as result1,
substring_index('www.baidu.com','.',2) as result2;
+---------+-----------+
| result1 | result2 |
+---------+-----------+
| www | www.baidu |
+---------+-----------+
1 row in set (0.00 sec)
mysql> select substring_index('www.baidu.com','.',-1) as result1,
substring_index('www.baidu.com','.',-2) as result2;
+---------+-----------+
| result1 | result2 |
+---------+-----------+
| com | baidu.com |
+---------+-----------+
1 row in set (0.00 sec)
十八、reverse()函数
reverse()函数的语法如下:
reverse(str);
说明:返回str的翻转字符串。
举例:
mysql> select reverse('I am a Student.') as result;
+-----------------+
| result |
+-----------------+
| .tnedutS a ma I |
+-----------------+
1 row in set (0.00 sec)
十九、strcmp()函数
strcmp()函数的语法如下:
strcmp(str1,str2);
说明:根据比较规则,如果字符串str1大于str2,返回1,如果字符串str1小于str2,返回-1,如果字符串str1和str2完全相同,则返回0。
举例:
mysql> select strcmp('abc','abc') as result;
+--------+
| result |
+--------+
| 0 |
+--------+
1 row in set (0.00 sec)
mysql> select strcmp('bc','abc') as result;
+--------+
| result |
+--------+
| 1 |
+--------+
1 row in set (0.01 sec)
mysql> select strcmp('abc','bc') as result;
+--------+
| result |
+--------+
| -1 |
+--------+
1 row in set (0.00 sec)