04 MySQL之函数
Posted huangyanpeng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了04 MySQL之函数相关的知识,希望对你有一定的参考价值。
01-数学函数
数学函数主要用来处理数值数据。
# 1、绝对值函数 ABS(x) 和 返回圆周率的函数 PI() ABS(x) 返回x的绝对值。 例: mysql> select ABS(2), ABS(-32), ABS(-2.3); +--------+----------+-----------+ | ABS(2) | ABS(-32) | ABS(-2.3) | +--------+----------+-----------+ | 2 | 32 | 2.3 | +--------+----------+-----------+ 1 row in set (0.01 sec) PI() 返回 3.14....的值。默认显示6位小数。 例: mysql> select pi(); +----------+ | pi() | +----------+ | 3.141593 | +----------+ 1 row in set (0.00 sec) # 2、平方根函数SQRT(x) 和 求余函数MOD(x, y) SQRT(x) 返回非负数x的二次方根。 例: mysql> select sqrt(9), sqrt(25), sqrt(-49); +---------+----------+-----------+ | sqrt(9) | sqrt(25) | sqrt(-49) | +---------+----------+-----------+ | 3 | 5 | NULL | +---------+----------+-----------+ 1 row in set (0.00 sec) MOD(x, y) 返回x除以y的余数。 例: mysql> select mod(41, 7), mod(27.5, 5), mod(45, 10); +------------+--------------+-------------+ | mod(41, 7) | mod(27.5, 5) | mod(45, 10) | +------------+--------------+-------------+ | 6 | 2.5 | 5 | +------------+--------------+-------------+ 1 row in set (0.00 sec) # 3、获取整数的函数CEIL(x)、CEILING(x)、FLOOR(x) CEIL(x)、CEILING(x) 意义相同,返回不小于x的最小整数,返回值转化为一个BIGINT。 例: mysql> select ceil(-3.4), ceil(-3.7), ceiling(-3.7), ceil(3.4); +------------+------------+---------------+-----------+ | ceil(-3.4) | ceil(-3.7) | ceiling(-3.7) | ceil(3.4) | +------------+------------+---------------+-----------+ | -3 | -3 | -3 | 4 | +------------+------------+---------------+-----------+ 1 row in set (0.00 sec) FLOOR(x) 返回不大于x的最大整数值。返回值转化为一个BIGINT。 例: mysql> select floor(3.4), floor(3), floor(-3.4), floor(-3.7), floor(3.7); +------------+----------+-------------+-------------+------------+ | floor(3.4) | floor(3) | floor(-3.4) | floor(-3.7) | floor(3.7) | +------------+----------+-------------+-------------+------------+ | 3 | 3 | -4 | -4 | 3 | +------------+----------+-------------+-------------+------------+ 1 row in set (0.00 sec) # 4、获取随机数的函数 RAND() 和 RAND(x) 例: mysql> select rand(), rand(), rand(); +---------------------+---------------------+--------------------+ | rand() | rand() | rand() | +---------------------+---------------------+--------------------+ | 0.25413118047093153 | 0.40886388850292554 | 0.2819259318354781 | +---------------------+---------------------+--------------------+ 1 row in set (0.00 sec) 不带参数的RAND()每次产生的随机数值是不同的。 mysql> select rand(5), rand(5), rand(10); +---------------------+---------------------+--------------------+ | rand(5) | rand(5) | rand(10) | +---------------------+---------------------+--------------------+ | 0.40613597483014313 | 0.40613597483014313 | 0.6570515219653505 | +---------------------+---------------------+--------------------+ 1 row in set (0.00 sec) 当RAND()的参数相同时,将产生相同的随机数,不同的x产生的随机数也不同。 # 5、函数ROUND(x)、ROUND(x, y)和TRUNCATE(x,y) ROUND(x) 返回最接近于参数x的整数,对x值进行四舍五入。 例: mysql> select round(-1.5),round(-1.45),round(-1.6),round(1.5),round(1.55),round(2.1); +-------------+--------------+-------------+------------+-------------+------------+ | round(-1.5) | round(-1.45) | round(-1.6) | round(1.5) | round(1.55) | round(2.1) | +-------------+--------------+-------------+------------+-------------+------------+ | -2 | -1 | -2 | 2 | 2 | 2 | +-------------+--------------+-------------+------------+-------------+------------+ 1 row in set (0.00 sec) ROUND(x) 返回最接近于参数x的整数,对x值进行四舍五入,结果保留小数点后面y位。 例: mysql> select round(-1.5, 1),round(-1.45, 3),round(-1.6, 0),round(1.5, 5),round(1.55); +----------------+-----------------+----------------+---------------+-------------+ | round(-1.5, 1) | round(-1.45, 3) | round(-1.6, 0) | round(1.5, 5) | round(1.55) | +----------------+-----------------+----------------+---------------+-------------+ | -1.5 | -1.450 | -2 | 1.50000 | 2 | +----------------+-----------------+----------------+---------------+-------------+ 1 row in set (0.00 sec) 当 y 值为负数时,保留小数点左边的相应位数直接保存为0,不进行四舍五入。 例: mysql> select round(1234.5, -1),round(123.45, -2),round(123.45, -3),round(12345.6, -4); +-------------------+-------------------+-------------------+--------------------+ | round(1234.5, -1) | round(123.45, -2) | round(123.45, -3) | round(12345.6, -4) | +-------------------+-------------------+-------------------+--------------------+ | 1230 | 100 | 0 | 10000 | +-------------------+-------------------+-------------------+--------------------+ 1 row in set (0.00 sec) TRUNCATE(x,y) 返回被舍去至小数点后y位的数字x。不进行四舍五入。 例: mysql> select truncate(123.456, -1), truncate(123.456, 1),truncate(123.456, -2), truncate(123.456, 2),truncate(123.456, 4); +-----------------------+----------------------+-----------------------+----------------------+----------------------+ | truncate(123.456, -1) | truncate(123.456, 1) | truncate(123.456, -2) | truncate(123.456, 2) | truncate(123.456, 4) | +-----------------------+----------------------+-----------------------+----------------------+----------------------+ | 120 | 123.4 | 100 | 123.45 | 123.4560 | +-----------------------+----------------------+-----------------------+----------------------+----------------------+ 1 row in set (0.00 sec) # 6、符号函数 SIGN(x) SIGN(x) 返回参数的符号,x的值为负,0或正时,返回结果依次为 -1、0 或 1。 例: mysql> select sign(-2), sign(-8.78), sign(3.1), sign(0); +----------+-------------+-----------+---------+ | sign(-2) | sign(-8.78) | sign(3.1) | sign(0) | +----------+-------------+-----------+---------+ | -1 | -1 | 1 | 0 | +----------+-------------+-----------+---------+ 1 row in set (0.00 sec) # 7、幂运算函数 POW(x, y)、POWER(x, y)和 EXP(x) POW(x, y)、POWER(x, y) 返回x的y次方。 EXP(x) 返回 e 的x乘方后的值。 例: mysql> select pow(2, 3), power(2, 3), exp(2); +-----------+-------------+------------------+ | pow(2, 3) | power(2, 3) | exp(2) | +-----------+-------------+------------------+ | 8 | 8 | 7.38905609893065 | +-----------+-------------+------------------+ 1 row in set (0.01 sec) # 8、对数函数LOG(x) 和 LOG10(x) LOG(x) 返回x的自然对数,以e为底的。 例: mysql> select log(5), log10(100); +--------------------+------------+ | log(5) | log10(100) | +--------------------+------------+ | 1.6094379124341003 | 2 | +--------------------+------------+ 1 row in set (0.00 sec) # 9、正弦函数 SIN(x) 和 反正弦函数 ASIN(x) SIN(x) 返回x正弦,x为弧度值。
02-字符串函数
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, ...)
03-日期与时间函数
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)
04-条件判断函数
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函数
05-系统信息函数
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)
06-加/解密函数
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之函数的主要内容,如果未能解决你的问题,请参考以下文章
在 Visual Studio 中创建构造函数的代码片段或快捷方式