时间:2021-07-01 10:21:17 帮助过:11人阅读
2.1 计算字符串字符数的函数和字符串长度的函数
CHAR_LENGTH(str) 返回字符串str 字符的个数。 例: mysql> select char_length(‘hyp‘), char_length(‘date‘), char_length(‘data_test‘); +--------------------+---------------------+--------------------------+ | char_length(‘hyp‘) | char_length(‘date‘) | char_length(‘data_test‘) | +--------------------+---------------------+--------------------------+ | 3 | 4 | 9 | +--------------------+---------------------+--------------------------+ 1 row in set (0.01 sec)
2.2 合并字符串长度 CONCAT(s1, s2, s3, ...)、CONCAT_WS(x, s1, s2, s3, ...)
CONCAT(s1, s2, s3, ...) 结果为连接参数产生的字符串。 CONCAT_WS(x, s1, s2, s3, ...) x为分隔符。 例: mysql> select concat(‘data‘, ‘test‘), concat(‘data‘, ‘ test‘), concat_ws(‘_‘,‘data‘, ‘test‘); +------------------------+-------------------------+-------------------------------+ | concat(‘data‘, ‘test‘) | concat(‘data‘, ‘ test‘) | concat_ws(‘_‘,‘data‘, ‘test‘) | +------------------------+-------------------------+-------------------------------+ | datatest | data test | data_test | +------------------------+-------------------------+-------------------------------+ 1 row in set (0.00 sec)
2.3 替换字符串函数 INSERT(s1, x, len, s2)
INSERT(s1, x, len, s2) 返回字符串s1,其子字符串起始于x位置和被字符串s2取代的len字符。如果x大于字符串长度,则返回值为原始字符串。若任何一个参数为NULL,则返回值为NULL。 例: mysql> select insert(‘data_test‘, 2, 4, ‘xxxx‘), insert(‘data_test‘, 2, 8, ‘xxxx‘), insert(‘data_test‘, 2, 4, null); +-----------------------------------+-----------------------------------+---------------------------------+ | insert(‘data_test‘, 2, 4, ‘xxxx‘) | insert(‘data_test‘, 2, 8, ‘xxxx‘) | insert(‘data_test‘, 2, 4, null) | +-----------------------------------+-----------------------------------+---------------------------------+ | dxxxxtest | dxxxx | NULL | +-----------------------------------+-----------------------------------+---------------------------------+ 1 row in set (0.00 sec)
2.4 字母大小写转换函数 LOWER(str)
将所有字符都转换成小写字母。 例: mysql> select lower(‘DATA‘), lower(‘Data‘),lower(‘DaTa‘); +---------------+---------------+---------------+ | lower(‘DATA‘) | lower(‘Data‘) | lower(‘DaTa‘) | +---------------+---------------+---------------+ | data | data | data | +---------------+---------------+---------------+ 1 row in set (0.00 sec)
2.5 获取指定长度的字符串 LEFT(s, n) 和 RIGHT(s, n)
LEFT(s, n) 返回字符串s 开始的最左边 n 个字符。 RIGHT(s, n) 返回字符串s 最右边 n 个字符。 例: mysql> select left(‘data_test‘, 3), left(‘data_test‘, 5), right(‘data_test‘, 3); +----------------------+----------------------+-----------------------+ | left(‘data_test‘, 3) | left(‘data_test‘, 5) | right(‘data_test‘, 3) | +----------------------+----------------------+-----------------------+ | dat | data_ | est | +----------------------+----------------------+-----------------------+ 1 row in set (0.00 sec)
2.6 填充字符串函数 LPAD(Ss1, len, s2)、RPAD(Ss1, len, s2)
LPAD(Ss1, len, s2) 返回字符串s1,其左边由字符串s2填补到len字符长度。如果s1的长度大于len,则返回值被缩短至len字符串。 RPAD(Ss1, len, s2) 返回字符串s1,其右边由字符串s2填补到len字符长度。如果s1的长度大于len,则返回值被缩短至len字符串。 例: mysql> select lpad(‘hello‘, 4, ‘??‘), lpad(‘hello‘, 10, ‘??‘),rpad(‘hello‘, 10, ‘??‘),rpad(‘hello‘, 4, ‘??‘); +------------------------+-------------------------+-------------------------+------------------------+ | lpad(‘hello‘, 4, ‘??‘) | lpad(‘hello‘, 10, ‘??‘) | rpad(‘hello‘, 10, ‘??‘) | rpad(‘hello‘, 4, ‘??‘) | +------------------------+-------------------------+-------------------------+------------------------+ | hell | ?????hello | hello????? | hell | +------------------------+-------------------------+-------------------------+------------------------+ 1 row in set (0.00 sec)
2.7 删除空格的函数 LTRIM(s)、RTRIM(s) 和 TRIM(s)
LTRIM(s) 返回字符串s左侧空格被删除的值。 RTRIM(s) 返回字符串s右侧空格被删除的值。 TRIM(s) 返回字符串s 两侧空格被删除的值。 例: mysql> select ltrim(‘ data ‘), rtrim(‘ data ‘), trim(‘ data ‘); +-------------------+-------------------+------------------+ | ltrim(‘ data ‘) | rtrim(‘ data ‘) | trim(‘ data ‘) | +-------------------+-------------------+------------------+ | data | data | data | +-------------------+-------------------+------------------+ 1 row in set (0.00 sec)
2.8 删除指定字符串函数 TRIM(s1 from s)
删除字符串 s 中两端所有的子字符串s1。 而不删除中间的 s1 例: mysql> select trim(‘aa‘ from ‘qweaaasewraaerfdaartyaa‘), trim(‘aa‘ from ‘aaqweaaasewraaerfdaartyaa‘); +-------------------------------------------+---------------------------------------------+ | trim(‘aa‘ from ‘qweaaasewraaerfdaartyaa‘) | trim(‘aa‘ from ‘aaqweaaasewraaerfdaartyaa‘) | +-------------------------------------------+---------------------------------------------+ | qweaaasewraaerfdaarty | qweaaasewraaerfdaarty | +-------------------------------------------+---------------------------------------------+ 1 row in set (0.00 sec)
2.9 重复删除字符串函数 REPEAT(s, n)
返回一个有重复字符串s组成的字符串。 例: mysql> select repeat(‘MySQL‘, 3); +--------------------+ | repeat(‘MySQL‘, 3) | +--------------------+ | MySQLMySQLMySQL | +--------------------+ 1 row in set (0.00 sec)
2.10 空格函数 SPACE(n) 和 替换函数 REPLACE(s, s1, s2)
SPACE(n) 返回一个由n个空格组成的字符串。 REPLACE(s, s1, s2) 使用s2代替字符串s中所有的s1。 例: mysql> select space(5), replace(‘data_xxx‘, ‘a‘, ‘w‘); +----------+-------------------------------+ | space(5) | replace(‘data_xxx‘, ‘a‘, ‘w‘) | +----------+-------------------------------+ | | dwtw_xxx | +----------+-------------------------------+ 1 row in set (0.00 sec)
2.11 比较字符串大小的函数 STRCMP(s1, s2)
STRCMP(s1, s2) 若字符串都相同,则返回0;第一个参数小于第二个,则返回-1,其他情况返回1。 例: mysql> select strcmp(‘data‘, ‘data‘), strcmp(‘data‘, ‘data2‘), strcmp(‘data3‘, ‘data‘), strcmp(‘test5‘, ‘data‘); +------------------------+-------------------------+-------------------------+-------------------------+ | strcmp(‘data‘, ‘data‘) | strcmp(‘data‘, ‘data2‘) | strcmp(‘data3‘, ‘data‘) | strcmp(‘test5‘, ‘data‘) | +------------------------+-------------------------+-------------------------+-------------------------+ | 0 | -1 | 1 | 1 | +------------------------+-------------------------+-------------------------+-------------------------+ 1 row in set (0.00 sec)
2.12 获取子串的函数 SUBSTRING(s, n, len) 和 MID(s, n, len)
SUBSTRING(s, n, len) 从字符串s返回一个长度同len字符相同的子字符串,起始于位置n。 例: mysql> select substring(‘leanmysql‘, 4), substring(‘leanmysql‘, 2, 4), substring(‘leanmysql‘, -5, 3); +---------------------------+------------------------------+-------------------------------+ | substring(‘leanmysql‘, 4) | substring(‘leanmysql‘, 2, 4) | substring(‘leanmysql‘, -5, 3) | +---------------------------+------------------------------+-------------------------------+ | nmysql | eanm | mys | +---------------------------+------------------------------+-------------------------------+ 1 row in set (0.00 sec) MID(s, n, len) 与 SUBSTRING(s, n, len) 作用相同。
2.13 匹配子串开始位置的函数 LOCATE(str1, str)、POSITION(str1 IN str) 和 INSTR(str, str1)
3个函数的作用相同,返回子字符串str1在字符串str中的开始位置。 例: mysql> select locate(‘el‘, ‘helllo‘); +------------------------+ | locate(‘el‘, ‘helllo‘) | +------------------------+ | 2 | +------------------------+ 1 row in set (0.00 sec)
2.14 字符串逆序的函数 REVERSE(s)
将字符串s反转。 例: mysql> select reverse(‘hello‘); +------------------+ | reverse(‘hello‘) | +------------------+ | olleh | +------------------+ 1 row in set (0.00 sec)
2.15 返回指定位置的字符串函数 ELT(N, str1, str2, str3, ...)
若N=1,则返回值为str1,以此类推。 例: mysql> select elt(2, ‘a‘, ‘b‘, ‘c‘), elt(4, ‘a‘, ‘b‘, ‘c‘); +-----------------------+-----------------------+ | elt(2, ‘a‘, ‘b‘, ‘c‘) | elt(4, ‘a‘, ‘b‘, ‘c‘) | +-----------------------+-----------------------+ | b | NULL | +-----------------------+-----------------------+ 1 row in set (0.00 sec)
2.16 返回指定字符串位置的函数 FIELD(s, s1, s2, s3, ...)
返回字符串s在s1, s2, s3, .....中第一次出现的位置,在找不到s的情况下,返回值为0。 例: mysql> select field(‘a‘, ‘data‘, ‘cbda‘); +----------------------------+ | field(‘a‘, ‘data‘, ‘cbda‘) | +----------------------------+ | 0 | +----------------------------+ 1 row in set (0.00 sec) mysql> select field(‘a‘, ‘hello‘, ‘a‘, ‘very‘); +----------------------------------+ | field(‘a‘, ‘hello‘, ‘a‘, ‘very‘) | +----------------------------------+ | 2 | +----------------------------------+ 1 row in set (0.00 sec)
2.17 返回子字符串位置的函数 FIND_IN_SET(s1, s2)
返回s1在s2中出现的位置。s2是个字符串列表,由 逗号 隔开。 例: mysql> select find_in_set(‘e‘, ‘hello‘); +---------------------------+ | find_in_set(‘e‘, ‘hello‘) | +---------------------------+ | 0 | +---------------------------+ 1 row in set (0.00 sec) mysql> select find_in_set(‘e‘, ‘h,e,ll,o‘); +------------------------------+ | find_in_set(‘e‘, ‘h,e,ll,o‘) | +------------------------------+ | 2 | +------------------------------+ 1 row in set (0.00 sec)
2.18 选取字符串的函数 MAKE_SET(x, s1, s2, ...)
3.1 获取当前日期的函数 和 获取当前时间的函数
CURDATE() 和 CURRENT_DATE() 的作用相同,将当前日期按照 ‘YYYY-MM-DD‘ 或 YYYYMMDD 格式的值返回。 例: mysql> select curdate(), current_date(), curdate()+0; +------------+----------------+-------------+ | curdate() | current_date() | curdate()+0 | +------------+----------------+-------------+ | 2019-07-23 | 2019-07-23 | 20190723 | +------------+----------------+-------------+ 1 row in set (0.02 sec) 注:CURDATE()+0 将当前日期值转换为数值型。 CURTIME() 和 CURRENT_TIME() 将当前时间以 ‘HH:MM:SS‘ 或 HHMMSS的格式返回。 例: mysql> select curtime(), current_time(), curtime()+0; +-----------+----------------+-------------+ | curtime() | current_time() | curtime()+0 | +-----------+----------------+-------------+ | 15:52:13 | 15:52:13 | 155213 | +-----------+----------------+-------------+ 1 row in set (0.00 sec)
3.2 获取当前日期和时间的函数
CURRENT_TIMESTAMP()、LOCALTIME()、NOW()、SYSDATE() 4个函数的作用相同,均返回当前日期和时间的值。格式为:‘YYYY-MM-DD HH:MM:SS‘ 或 YYYYMMDDHHMMSS。 例: mysql> select current_timestamp(), localtime(); +---------------------+---------------------+ | current_timestamp() | localtime() | +---------------------+---------------------+ | 2019-10-23 15:57:24 | 2019-10-23 15:57:24 | +---------------------+---------------------+ 1 row in set (0.00 sec) mysql> select now(), sysdate(); +---------------------+---------------------+ | now() | sysdate() | +---------------------+---------------------+ | 2019-10-23 15:57:42 | 2019-10-23 15:57:42 | +---------------------+---------------------+ 1 row in set (0.00 sec)
3.3 UNIX时间戳函数
UTC_TIMESTAMP(date)
3.4 返回UTC日期的函数和返回UTC日期的函数
UTC_DATE() UTC_TIME() 例: mysql> select utc_date(), utc_date()+0; +------------+--------------+ | utc_date() | utc_date()+0 | +------------+--------------+ | 2019-10-23 | 20191023 | +------------+--------------+ 1 row in set (0.00 sec) mysql> select utc_time(), utc_time()+0; +------------+--------------+ | utc_time() | utc_time()+0 | +------------+--------------+ | 08:00:45 | 80045 | +------------+--------------+ 1 row in set (0.00 sec)
3.5 获取月份的函数 MONTH(date)和 MONTHNAME(date)
mysql> select month(‘2019-09-10‘), monthname(‘2019-09-10‘); +---------------------+-------------------------+ | month(‘2019-09-10‘) | monthname(‘2019-09-10‘) | +---------------------+-------------------------+ | 9 | September | +---------------------+-------------------------+ 1 row in set (0.00 sec)
3.6 获取星期的函数 DAYNAME(d)、 DAYOFWEEK(d)、 WEEKDAY(d)
mysql> select dayname(‘2019-09-09‘), dayofweek(‘2019-09-09‘), weekday(‘2019-09-09‘); +-----------------------+-------------------------+-----------------------+ | dayname(‘2019-09-09‘) | dayofweek(‘2019-09-09‘) | weekday(‘2019-09-09‘) | +-----------------------+-------------------------+-----------------------+ | Monday | 2 | 0 | +-----------------------+-------------------------+-----------------------+ 1 row in set (0.00 sec)
3.7 获取星期数的函数 WEEK(d)、WEEKOFYEAR(d)
mysql> select week(‘2019-09-09‘), weekofyear(‘2019-09-09‘); +--------------------+--------------------------+ | week(‘2019-09-09‘) | weekofyear(‘2019-09-09‘) | +--------------------+--------------------------+ | 36 | 37 | +--------------------+--------------------------+ 1 row in set (0.00 sec)
3.8 获取天数的函数 DAYOFYEAR(d)、DAYOFMONTH(d)
mysql> select dayofyear(‘2019-09-09‘), dayofmonth(‘2019-09-09‘); +-------------------------+--------------------------+ | dayofyear(‘2019-09-09‘) | dayofmonth(‘2019-09-09‘) | +-------------------------+--------------------------+ | 252 | 9 | +-------------------------+--------------------------+ 1 row in set (0.00 sec)
3.9 获取年份、季度、小时、分钟 和 秒钟的函数
mysql> select year(‘2019-09-09‘), quarter(‘2019-09-09‘), minute(‘07:08:10‘), second(‘07:08:10‘); +--------------------+-----------------------+--------------------+--------------------+ | year(‘2019-09-09‘) | quarter(‘2019-09-09‘) | minute(‘07:08:10‘) | second(‘07:08:10‘) | +--------------------+-----------------------+--------------------+--------------------+ | 2019 | 3 | 8 | 10 | +--------------------+-----------------------+--------------------+--------------------+ 1 row in set (0.00 sec)
3.10 获取日期指定值的函数 EXTRACT(type from date)
3.11 计算日期与时间的函数
3.12 将日期与时间格式化的函数 DATE_FORMAT(date, format)
4.1 IF(expr, v1, v2)
mysql> select if(1>2, ‘yes‘, ‘no‘); +----------------------+ | if(1>2, ‘yes‘, ‘no‘) | +----------------------+ | no | +----------------------+ 1 row in set (0.00 sec)
4.2 IFNULL(v1, v2)函数
mysql> select ifnull(1, 2), ifnull(null, 2); +--------------+-----------------+ | ifnull(1, 2) | ifnull(null, 2) | +--------------+-----------------+ | 1 | 2 | +--------------+-----------------+ 1 row in set (0.01 sec)
4.3 CASE函数
5.1 获取MySQL版本号、连接数和数据库名的函数
mysql> select version(), connection_id(); +-----------+-----------------+ | version() | connection_id() | +-----------+-----------------+ | 5.7.28 | 5 | +-----------+-----------------+ 1 row in set (0.00 sec) mysql> show full processlist; +----+--------------+-----------+---------+---------+------+----------+-----------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+--------------+-----------+---------+---------+------+----------+-----------------------+ | 5 | xxxxxxxxx | localhost | test_db | Query | 0 | starting | show full processlist | +----+--------------+-----------+---------+---------+------+----------+-----------------------+ 1 row in set (0.01 sec) mysql> select database(), schema(); +------------+----------+ | database() | schema() | +------------+----------+ | test_db | test_db | +------------+----------+ 1 row in set (0.00 sec)
5.2 获取用户名的函数
mysql> select user(), current_user(), system_user(); +---------------+-----------------------------------+---------------+ | user() | current_user() | system_user() | +---------------+-----------------------------------+---------------+ | huangyanpeng@ | skip-grants user@skip-grants host | huangyanpeng@ | +---------------+-----------------------------------+---------------+ 1 row in set (0.00 sec)
5.3 获取字符串的字符集和排序方式的函数
mysql> select charset(‘abc‘); +----------------+ | charset(‘abc‘) | +----------------+ | utf8 | +----------------+ 1 row in set (0.00 sec) COLLATION() 返回字符串的排列方式
5.4 获取最后一个自动生成ID值的函数
LAST_INSERT_ID() 自动返回最后一个insert、update或auto_increment列设置的第一个发生的值。 mysql> select last_insert_id(); +------------------+ | last_insert_id() | +------------------+ | 0 | +------------------+ 1 row in set (0.00 sec)
6.1 PASSWORD(str)
mysql> select password(‘abc‘); +-------------------------------------------+ | password(‘abc‘) | +-------------------------------------------+ | *0D3CED9BEC10A777AEC23CCC353A8C08A633045E | +-------------------------------------------+ 1 row in set, 1 warning (0.01 sec)
6.2 加密函数 MD5(str)
mysql> select md5(‘abc‘); +----------------------------------+ | md5(‘abc‘) | +----------------------------------+ | 900150983cd24fb0d6963f7d28e17f72 | +----------------------------------+ 1 row in set (0.01 sec)
6.3 加密函数 ENCODE(str, pswd_str)
使用pswd_str作为密码,加密str。
6.4 解密函数 DECODE(crypt_str, pswd_str)
使用pswd_str作为密码,解密加密字符串crypt_str。
04 MySQL之函数
标签:版本 ifnull length pre hmm rand 条件判断 rem 大小写