MySQL字符串函数
1,UPPER和UCASE
把字符串转成大小写.
mysql> select name,UPPER(name) from string_test where name='tank'; +------+-------------+ | name | UPPER(name) | +------+-------------+ | tank | TANK | +------+-------------+ 1 row in set (0.00 sec)
mysql> select sex,LCASE(job) from string_test where job='DUCK'; +------+------------+ | sex | LCASE(job) | +------+------------+ | 1 | duck | +------+------------+ 1 row in set (0.00 sec)
3,FIND_IN_SET(str,strlist)
4,FIELD(str,str1,str2,str3,…)
返回str在str1, str2, str3, …清单的索引。如果str没找到,返回0。FIELD()是ELT()反运算。
mysql> SELECT id, name, FIELD( id, name, sex, job, hobby ) -> FROM string_test where id < 4; +----+-------+------------------------------------+ | id | name | FIELD( id, name, sex, job, hobby ) | +----+-------+------------------------------------+ | 1 | tank | 2 | | 2 | zhang | 0 | | 3 | ying | 0 | +----+-------+------------------------------------+ 3 rows in set (0.00 sec)
5.ELT(N,str1,str2,str3,…)
如果N= 1,返回str1,如果N= 2,返回str2,等等。如果N小于1或大于参数个数,返回NULL。ELT()是FIELD()反运算。
mysql> SELECT id, name, ELT(1, id, name, sex, job, hobby ) FROM string_test where id < 4; +----+-------+------------------------------------+ | id | name | ELT(1, id, name, sex, job, hobby ) | +----+-------+------------------------------------+ | 1 | tank | 1 | | 2 | zhang | 2 | | 3 | ying | 3 | +----+-------+------------------------------------+ 3 rows in set (0.00 sec)
mysql> SELECT id, name, ELT(2, id, name, sex, job, hobby ) FROM string_test where id < 4; +----+-------+------------------------------------+ | id | name | ELT(2, id, name, sex, job, hobby ) | +----+-------+------------------------------------+ | 1 | tank | tank | | 2 | zhang | zhang | | 3 | ying | ying | +----+-------+------------------------------------+ 3 rows in set (0.00 sec)
6,REPLACE(str,from_str,to_str)
返回字符串str,其字符串from_str的所有出现由字符串to_str代替。
mysql> SELECT id,REPLACE(hobby,"firend",'living') FROM string_test WHERE id = 2; +----+----------------------------------+ | id | REPLACE(hobby,"firend",'living') | +----+----------------------------------+ | 2 | fly,make living | +----+----------------------------------+ 1 row in set (0.00 sec)
7,REPEAT(str,count)
返回由重复countTimes次的字符串str组成的一个字符串。如果count <= 0,返回一个空字符串。如果str或count是NULL,返回NULL。
mysql> SELECT id,REPEAT(name,2) FROM string_test WHERE id > 1 and id < 4; +----+----------------+ | id | REPEAT(name,2) | +----+----------------+ | 2 | zhangzhang | | 3 | yingying | +----+----------------+ 2 rows in set (0.00 sec)
8,REVERSE(str)
返回颠倒字符顺序的字符串str。
mysql> SELECT id,reverse(name) FROM string_test WHERE id > 1 and id < 4; +----+---------------+ | id | reverse(name) | +----+---------------+ | 2 | gnahz | | 3 | gniy | +----+---------------+ 2 rows in set (0.00 sec)
9,INSERT(str,pos,len,newstr)
返回字符串str,在位置pos起始的子串且len个字符长得子串由字符串newstr代替。
mysql> select id,name,INSERT(hobby,10,6,'living') from string_test where id = 2; +----+-------+-----------------------------+ | id | name | INSERT(hobby,10,6,'living') | +----+-------+-----------------------------+ | 2 | zhang | fly,make living | +----+-------+-----------------------------+ 1 row in set (0.00 sec)
10,SUBSTRING(str FROM pos)
从字符串str的起始位置pos返回一个子串。下面的sub2没有值,因为mysql数据库的下标是从1开始的。
mysql> SELECT id, substring( hobby, 1, 6) AS sub1, substring( hobby from 0 for 8 ) AS sub2,substring( hobby,2) AS sub3, substring( hobby from 4 ) AS sub4 FROM s tring_test WHERE id =4; +----+--------+------+-----------+---------+ | id | sub1 | sub2 | sub3 | sub4 | +----+--------+------+-----------+---------+ | 4 | i love | | love you | ove you | +----+--------+------+-----------+---------+ 1 row in set (0.00 sec)
11,SUBSTRING_INDEX(str,delim,count)
返回从字符串str的第count个出现的分隔符delim之后的子串。如果count是正数,返回最后的分隔符到左边(从左边数) 的所有字符。如果count是负数,返回最后的分隔符到右边的所有字符(从右边数)。
mysql> SELECT id,SUBSTRING_INDEX(hobby,',',2) as test1,SUBSTRING_INDEX(hobby,',' ,-1) as test2 FROM string_test WHERE id = 3; +----+----------------+---------+ | id | test1 | test2 | +----+----------------+---------+ | 3 | flying,driving | testing | +----+----------------+---------+ 1 row in set (0.01 sec)
12,LTRIM(str)
返回删除了其前置空格字符的字符串str。
mysql> SELECT id,LTRIM(job) FROM string_test WHERE id = 4; +----+----------------+ | id | LTRIM(job) | +----+----------------+ | 4 | love your love | +----+----------------+ 1 row in set (0.00 sec)
13,RTRIM(str)
返回删除了其拖后空格字符的字符串str。
mysql> SELECT id,RTRIM(job) FROM string_test WHERE id = 4; +----+----------------+ | id | RTRIM(job) | +----+----------------+ | 4 | love your love | +----+----------------+ 1 row in set (0.00 sec)
14,TRIM([[BOTH | LEADING | TRAILING] [remstr] FROM] str)
返回字符串str,其所有remstr前缀或后缀被删除了。如果没有修饰符BOTH、LEADING或TRAILING给出,BOTH被假定。如果remstr没被指定,空格被删除。
mysql> select trim(' test '); +-----------------+ | trim(' test ') | +-----------------+ | test | +-----------------+ 1 row in set (0.01 sec)
mysql> SELECT id,TRIM(LEADING "love" from job) as test1,TRIM(BOTH "love" from jo b) as test2,TRIM(TRAILING "love" from job) as test3 FROM string_test WHERE id = 4 -> ; +----+------------+--------+------------+ | id | test1 | test2 | test3 | +----+------------+--------+------------+ | 4 | your love | your | love your | +----+------------+--------+------------+ 1 row in set (0.00 sec)
15,MID(str,pos,len)
从字符串str返回一个len个字符的子串,从位置pos开始。使用FROM的变种形式是ANSI SQL92语法。
mysql> SELECT id, mid( hobby, 1, 6 ) AS sub1, mid( hobby -> FROM 0 -> FOR 8 ) AS sub2, mid( hobby, 2 ) AS sub3, mid( hobby -> FROM 4 ) AS sub4 -> FROM string_test -> WHERE id =4 ; +----+--------+------+-----------+---------+ | id | sub1 | sub2 | sub3 | sub4 | +----+--------+------+-----------+---------+ | 4 | i love | | love you | ove you | +----+--------+------+-----------+---------+ 1 row in set (0.00 sec)
16,LPAD(str,len,padstr)
返回字符串str,左面用字符串padstr填补直到str是len个字符长。
mysql> SELECT id,LPAD(name,11,"zhang ") FROM string_test WHERE id = 3; +----+------------------------+ | id | LPAD(name,11,"zhang ") | +----+------------------------+ | 3 | zhang zying | +----+------------------------+ 1 row in set (0.00 sec)
17,RPAD(str,len,padstr)
返回字符串str,右面用字符串padstr填补直到str是len个字符长。
mysql> SELECT id,RPAD(name,11," ying") FROM string_test WHERE id = 2; +----+-----------------------+ | id | RPAD(name,11," ying") | +----+-----------------------+ | 2 | zhang ying | +----+-----------------------+ 1 row in set (0.00 sec)
18,LEFT(str,len)
返回字符串str的最左面len个字符。
mysql> SELECT id,left(job,4) FROM string_test WHERE id = 4; +----+-------------+ | id | left(job,4) | +----+-------------+ | 4 | love | +----+-------------+ 1 row in set (0.00 sec)
19,RIGHT(str,len)
返回字符串str的最右面len个字符。
mysql> SELECT id,right(job,4) FROM string_test WHERE id = 4; +----+--------------+ | id | right(job,4) | +----+--------------+ | 4 | love | +----+--------------+ 1 row in set (0.00 sec)
20,位置控制函数
POSITION(substr IN str)
返回子串substr在字符串str第一个出现的位置,如果substr不是在str里面,返回0.
LOCATE(substr,str,pos)
返回子串substr在字符串str第一个出现的位置,从位置pos开始。如果substr不是在str里面,返回0。
INSTR(str,substr)
返回子串substr在字符串str中的第一个出现的位置。这与有2个参数形式的LOCATE()相同,除了参数被颠倒。
mysql> SELECT id,INSTR(job,"you") as instr,LOCATE('love',job,3) as locate,POSITI ON('love' in job) as position FROM string_test WHERE id = 4; +----+-------+--------+----------+ | id | instr | locate | position | +----+-------+--------+----------+ | 4 | 6 | 11 | 1 | +----+-------+--------+----------+ 1 row in set (0.00 sec)
21,得到字符串长度的函数
LENGTH(str),OCTET_LENGTH(str),CHAR_LENGTH(str),CHARACTER_LENGTH(str)
mysql> SELECT id,LENGTH(job) as one,OCTET_LENGTH(job) as two,CHAR_LENGTH(job) as three,CHARACTER_LENGTH(job) as four FROM string_test WHERE id = 4; +----+-----+-----+-------+------+ | id | one | two | three | four | +----+-----+-----+-------+------+ | 4 | 14 | 14 | 14 | 14 | +----+-----+-----+-------+------+ 1 row in set (0.00 sec)
22,合并多个字符串,或者表中的多个字段
CONCAT(str1,str2,…)
返回来自于参数连结的字符串。如果任何参数是NULL,返回NULL。可以有超过2个的参数。一个数字参数被变换为等价的字符串形式。
mysql> SELECT id,CONCAT(name,job,hobby) FROM string_test WHERE id = 4; +----+-----------------------------------+ | id | CONCAT(name,job,hobby) | +----+-----------------------------------+ | 4 | tankzhanglove your lovei love you | +----+-----------------------------------+ 1 row in set (0.00 sec)
23,进制转换
BIN(N)
返回二进制值N的一个字符串表示,在此N是一个长整数(BIGINT)数字,这等价于CONV(N,10,2)。如果N是NULL,返回NULL。
OCT(N)
返回八进制值N的一个字符串的表示,在此N是一个长整型数字,这等价于CONV(N,10,8)。如果N是NULL,返回NULL。
HEX(N)
返回十六进制值N一个字符串的表示,在此N是一个长整型(BIGINT)数字,这等价于CONV(N,10,16)。如果N是NULL,返回NULL。
ASCII(str)
返回字符串str的最左面字符的ASCII代码值。如果str是空字符串,返回0。如果str是NULL,返回NULL。
mysql> select bin(20),oct(20),hex(20),ascii(20); +---------+---------+---------+-----------+ | bin(20) | oct(20) | hex(20) | ascii(20) | +---------+---------+---------+-----------+ | 10100 | 24 | 14 | 50 | +---------+---------+---------+-----------+ 1 row in set (0.02 sec)
上面我只例举了一部分对字符串进行操作的函数,并且是我觉得我们平时会用的,有可能会用到的一些函数。
在MySQL中进行字符串的拼接要使用CONCAT()函数,CONCAT()函数支持一个或者多个参数,参数类型可以为字符串类型也可以是非字符串类 型。对于非字符串类型的参数MySQL将尝试将其转化为字符串类型,CONCAT()函数会将所有参数按照参数的顺序拼接成一个字符串作为返回值。比如下 面的SQL语句用于将用户的多个字段信息以一个字段的形式查询出来:
SELECT CONCAT('Staff Number :', 'Happiness Index', FNumber, FSalary/(FAge-21)) from t_employee +----------------------------------------------------------+ | concat | +----------------------------------------------------------+ | Staff number: Happiness index of DEV001 is 2075.000000 | | Staff number: Happiness index of DEV002 is 328.571429 | | NULL | | Staff number: Happiness index of HR001 is 2150.000000 | | Staff number: Happiness index of HR002 is 825.000000 | | Staff number: Happiness index of IT001 is 785.714286 | | Staff number: Happiness index of IT002 is 466.666667 | | Staff number: Happiness index of SALES001 is 1325.000000 | | Staff number: Happiness index of SALES002 is 592.857143 | +----------------------------------------------------------+
MYSQL中还提供了另外一个进行字符串拼接的函数CONCAT_WS,CONCAT_WS可以在待拼接的字符串之间加入指定的分割符。它的第一个参数值为采用的分隔符,而剩下的参数则为待拼接的字符串值,比如执行下面的SQL:
SELECT CONCAT_WS(', ', FNumber,FAge, FDepartment, FSalary) FROM T_Employee +------------------------------------------------------+ | concat_ws(', ', FNumber, FAge, FDepartment, FSalary) | +------------------------------------------------------+ | DEV001, 25, Development, 8300.00 | | DEV002, 28, Development, 2300.00 | | DEV003, Development, 3333.00 | | HR001, 23, HumanResource, 4300.00 | | HR002, 25, HumanResource, 3300.00 | | IT001, 28, InfoTech, 5500.00 | | IT002, 27, InfoTech, 2800.00 | | SALES001, 25, Sales, 5300.00 | | SALES002, 35, Sales, 8300.00 | +------------------------------------------------------+