select now(), curdate(), curtime()
select adddate('2007-02-02', interval 31 day)
select round(2.345,2), truncate(2.345,2), mod(323,5)
结合MySQL自带的帮助文档列一下MySQL数据库中常用的一些函数。
事实证明:MySQL的联机帮助资料非常实用,希望哪一天可爱的Oracle可以像MySQL学习一下,她可以让您基本不用查看其他的资料就将函数的基本使用方法和示例尽收眼底。
废话少说,直入主题
一、常用字符串函数
1.CONCAT(str1,str2,...)
mysql>
? concat;
mysql> SELECT CONCAT('My', 'S',
'QL');
+-------------------------+
| CONCAT('My', 'S', 'QL')
|
+-------------------------+
| MySQL
|
+-------------------------+
2.INSERT(str,pos,len,newstr)
mysql>
? insert function;
mysql> SELECT INSERT('Quadratic', 3, 4,
'What');
+-----------------------------------+
| INSERT('Quadratic', 3, 4,
'What') |
+-----------------------------------+
|
QuWhattic
|
+-----------------------------------+
3.LOWER(str)
mysql> ?
lower
mysql> SELECT
LOWER('QUADRATICALLY');
+------------------------+
|
LOWER('QUADRATICALLY') |
+------------------------+
|
quadratically
|
+------------------------+
4.UPPER(str)
mysql> ?
upper
mysql> SELECT UPPER('Hej');
+--------------+
| UPPER('Hej')
|
+--------------+
| HEJ
|
+--------------+
5.LEFT(str,len)
mysql> ? left
mysql>
SELECT LEFT('foobarbar', 5);
+----------------------+
| LEFT('foobarbar',
5) |
+----------------------+
| fooba
|
+----------------------+
6.RIGHT(str,len)
mysql> ?
right
mysql> SELECT RIGHT('foobarbar',
4);
+-----------------------+
| RIGHT('foobarbar', 4)
|
+-----------------------+
| rbar
|
+-----------------------+
7.LPAD(str,len,padstr)
mysql> ?
lpad
mysql> SELECT LPAD('hi',4,'??');
+-------------------+
|
LPAD('hi',4,'??') |
+-------------------+
| ??hi
|
+-------------------+
8.RPAD(str,len,padstr)
mysql> ?
rpad
mysql> SELECT RPAD('hi',5,'?');
+------------------+
|
RPAD('hi',5,'?') |
+------------------+
| hi???
|
+------------------+
9.LTRIM(str)
mysql> ? ltrim
mysql>
SELECT LTRIM(' barbar');
+-------------------+
| LTRIM(' barbar')
|
+-------------------+
| barbar
|
+-------------------+
10.RTRIM(str)
mysql> ?
rtrim
mysql> SELECT RTRIM('barbar ');
+--------------------+
|
RTRIM('barbar ') |
+--------------------+
| barbar
|
+--------------------+
11.TRIM(str)
mysql> ? trim
mysql>
SELECT TRIM(' bar ');
+------------------+
| TRIM(' bar ')
|
+------------------+
| bar
|
+------------------+
12.REPEAT(str,count)
mysql> ? repeat
function;
mysql> SELECT REPEAT('MySQL', 3);
+--------------------+
|
REPEAT('MySQL', 3) |
+--------------------+
| MySQLMySQLMySQL
|
+--------------------+
13.REPLACE(str,from_str,to_str)
mysql>
? replace
mysql> SELECT REPLACE('www.mysql.com', 'w',
'Ww');
+-------------------------------------+
| REPLACE('www.mysql.com',
'w', 'Ww') |
+-------------------------------------+
|
WwWwWw.mysql.com
|
+-------------------------------------+
14.STRCMP(expr1,expr2)
mysql>
? strcmp
mysql> SELECT STRCMP('text', 'text2'),STRCMP('text2',
'text'),STRCMP('text',
'text');
+-----------------------+-----------------------+----------------------+
|STRCMP('text',
'text2')|STRCMP('text2', 'text')|STRCMP('text',
'text')|
+-----------------------+-----------------------+----------------------+
|
-1 | 1|
0|
+-----------------------+-----------------------+----------------------+
15.SUBSTRING
mysql>
? substring
SUBSTRING(str,pos), SUBSTRING(str FROM pos),
SUBSTRING(str,pos,len),
SUBSTRING(str FROM pos FOR len)
mysql> SELECT
SUBSTRING('Secooler',3,4),
SUBSTRING('Secooler',6);
+---------------------------+-------------------------+
|
SUBSTRING('Secooler',3,4) | SUBSTRING('Secooler',6)
|
+---------------------------+-------------------------+
|
cool | ler
|
+---------------------------+-------------------------+
二、数值函数
1.ABS(X)
取绝对值函数
mysql> ? abs
mysql> SELECT ABS(-32);
+----------+
|
ABS(-32) |
+----------+
| 32 |
+----------+
2.CEILING(X),
CEIL(X) 取天棚函数
mysql> ? ceil
mysql> SELECT CEILING(1.23),
CEIL(-1.23);
+---------------+-------------+
| CEILING(1.23) | CEIL(-1.23)
|
+---------------+-------------+
| 2 | -1
|
+---------------+-------------+
3.FLOOR(X) 取地板函数
mysql> ?
floor
mysql> SELECT
FLOOR(1.23),FLOOR(-1.23);
+-------------+--------------+
| FLOOR(1.23) |
FLOOR(-1.23) |
+-------------+--------------+
| 1 |
-2 |
+-------------+--------------+
4.MOD(N,M), N % M, N MOD M
取模函数
mysql> ? mod
mysql> SELECT MOD(234, 10), 253 % 7, MOD(29,9), 29
MOD 9;
+--------------+---------+-----------+----------+
| MOD(234, 10) |
253 % 7 | MOD(29,9) | 29 MOD 9
|
+--------------+---------+-----------+----------+
| 4
| 1 | 2 | 2
|
+--------------+---------+-----------+----------+
5.RAND(), RAND(N)
取0-1之间的随机数函数
mysql> ? rand
mysql> SELECT RAND(),
RAND();
+------------------+-----------------+
| RAND() |
RAND() |
+------------------+-----------------+
|
0.77874226009356 | 0.5317868818825
|
+------------------+-----------------+
6.TRUNCATE(X,D)
返回数字X被截断为D位小数的结果
mysql> ? truncate
mysql> SELECT TRUNCATE(1.223,1),
TRUNCATE(1.999,1),
TRUNCATE(-1.999,2);
+-------------------+-------------------+--------------------+
|
TRUNCATE(1.223,1) | TRUNCATE(1.999,1) | TRUNCATE(-1.999,2)
|
+-------------------+-------------------+--------------------+
|
1.2 | 1.9 | -1.99
|
+-------------------+-------------------+--------------------+
三、日期和时间函数
1.CURDATE()
当前日期函数
mysql> ? curdate
mysql> SELECT CURDATE(),CURDATE() +
0;
+------------+---------------+
| CURDATE() | CURDATE() + 0
|
+------------+---------------+
| 2009-07-03 | 20090703
|
+------------+---------------+
2.CURTIME() 当前时间函数
mysql> ?
curtime
mysql> SELECT CURTIME(), CURTIME() +
0;
+-----------+---------------+
| CURTIME() | CURTIME() + 0
|
+-----------+---------------+
| 12:07:08 | 120708
|
+-----------+---------------+
3.NOW() 当前日期和时间函数
mysql> ?
now
mysql> SELECT NOW(), NOW() +
0;
+---------------------+----------------+
| NOW() | NOW()
+ 0 |
+---------------------+----------------+
| 2009-07-03 12:07:54
| 20090703120754
|
+---------------------+----------------+
4.UNIX_TIMESTAMP(),
UNIX_TIMESTAMP(date) 日期date的时间戳
mysql> ? unix_timestamp
mysql>
SELECT UNIX_TIMESTAMP(), UNIX_TIMESTAMP('1981-02-15
23:23:00');
+------------------+---------------------------------------+
|
UNIX_TIMESTAMP() | UNIX_TIMESTAMP('1981-02-15 23:23:00')
|
+------------------+---------------------------------------+
|
1246594366 | 351098580
|
+------------------+---------------------------------------+
5.FROM_UNIXTIME(unix_timestamp),
FROM_UNIXTIME(unix_timestamp,format) 返回时间戳的日期值(UNIX_TIMESTAMP的反函数)
mysql>
? from_unixtime
mysql> SELECT
FROM_UNIXTIME(1246594135),FROM_UNIXTIME(351098580);
+---------------------------+--------------------------+
|
FROM_UNIXTIME(1246594135) | FROM_UNIXTIME(351098580)
|
+---------------------------+--------------------------+
| 2009-07-03
12:08:55 | 1981-02-15 23:23:00
|
+---------------------------+--------------------------+
6.WEEK(date[,mode])
返回所给日期是一年中的第几周
mysql> ? week
mysql> SELECT
WEEK('1981-02-15');
+--------------------+
| WEEK('1981-02-15')
|
+--------------------+
| 7
|
+--------------------+
7.YEAR(date)
mysql> ? year
mysql>
SELECT YEAR('81-02-15');
+------------------+
| YEAR('81-02-15')
|
+------------------+
| 1981
|
+------------------+
8.HOUR(time) 返回时间的小时信息
mysql> ?
hour
mysql> SELECT HOUR('10:05:03');
+------------------+
|
HOUR('10:05:03') |
+------------------+
| 10
|
+------------------+
9.MINUTE(time) 返回时间的分钟信息
mysql> SELECT
MINUTE('98-02-03 10:05:03');
+-----------------------------+
|
MINUTE('98-02-03 10:05:03')
|
+-----------------------------+
| 5
|
+-----------------------------+
10.MONTHNAME(date)
返回时间的完整月份名字
mysql> SELECT
MONTHNAME('1981-02-15');
+-------------------------+
|
MONTHNAME('1981-02-15') |
+-------------------------+
|
February
|
+-------------------------+
11.DATE_FORMAT(date,format)
根据format格式date显示形式
mysql> ? date_format
mysql> SELECT
DATE_FORMAT('1981-02-15 23:23:00', '%W %M
%Y');
+------------------------------------------------+
|
DATE_FORMAT('1981-02-15 23:23:00', '%W %M %Y')
|
+------------------------------------------------+
| Sunday February
1981
|
+------------------------------------------------+
12.DATE_ADD(date,INTERVAL
expr type) 返回与所给日期date相差INTERVAL的时间段
mysql> select now() current,
date_add(now(), INTERVAL 31 day)
after31days;
+---------------------+---------------------+
|
current | after31days
|
+---------------------+---------------------+
| 2009-07-03 12:34:15 |
2009-08-03 12:34:15
|
+---------------------+---------------------+
13.DATEDIFF(expr,expr2)
计算两个日期之间相差的天数
mysql> SELECT DATEDIFF(now(),'1981-02-15
23:23:00');
+---------------------------------------+
|
DATEDIFF(now(),'1981-02-15 23:23:00')
|
+---------------------------------------+
|
10365
|
+---------------------------------------+
四、MySQL控制流程函数
首先创建演示表salary
mysql>
use test;
mysql> create table salary (userid int, salary
decimal(9,2));
mysql> insert into salary values
(1,1000),(2,2000),(3,3000),(4,4000),(5,5000),(1,null);
mysql> select *
from salary;
+--------+---------+
| userid | salary
|
+--------+---------+
| 1 | 1000.00 |
| 2 | 2000.00
|
| 3 | 3000.00 |
| 4 | 4000.00 |
| 5 | 5000.00
|
| 1 | NULL
|
+--------+---------+
1.IF(expr1,expr2,expr3)函数:如果expr1为真则返回expr2,否则返回expr3
mysql>
? IF FUNCTION
mysql> select IF( salary > 2000, 'High', 'Low') from
salary;
+------------------------------------+
| if ( salary > 2000,
'High', 'Low') |
+------------------------------------+
|
Low |
| Low
|
| High |
|
High |
| High
|
| Low
|
+------------------------------------+
2.IFNULL(expr1,expr2)函数:若expr1为NULL则返回expr2内容
mysql>
? IFNULL
mysql> select ifnull(salary,0) from
salary;
+------------------+
| ifnull(salary,0)
|
+------------------+
| 1000.00 |
| 2000.00
|
| 3000.00 |
| 4000.00 |
| 5000.00
|
| 0.00
|
+------------------+
3.NULLIF(expr1,expr2)函数:若expr1 =
expr2则返回NULL,否则返回expr1
mysql> ? NULLIF
mysql> select
nullif(salary,2000) from salary;
+---------------------+
|
nullif(salary,2000) |
+---------------------+
| 1000.00
|
| NULL |
| 3000.00 |
|
4000.00 |
| 5000.00 |
| NULL
|
+---------------------+
4.CASE函数
语法如下:
CASE value WHEN
[compare_value] THEN result [WHEN [compare_value] THEN result ...] [ELSE result]
END
或
CASE WHEN [condition] THEN result [WHEN [condition] THEN result ...]
[ELSE result] END
mysql> ? CASE FUNCTION
mysql> select case when
salary <= 2000 then 'Low' else 'High' end from
salary;
+-----------------------------------------------------+
| case
when salary <= 2000 then 'Low' else 'High' end
|
+-----------------------------------------------------+
|
Low |
|
Low |
|
High |
|
High |
|
High |
|
High
|
+-----------------------------------------------------+
mysql>
select case salary when 1000 then 'Low' when 2000 then 'Mid' else 'High' end
from
salary;
+-----------------------------------------------------------------------+
|
case salary when 1000 then 'Low' when 2000 then 'Mid' else 'High' end
|
+-----------------------------------------------------------------------+
|
Low |
|
Mid |
|
High |
|
High |
|
High |
|
High
|
+-----------------------------------------------------------------------+
五、其他常用函数
1.DATABASE()函数:用于查询当前使用数据库的名字(类似Oracle的show
user;)
mysql> ? database
mysql> SELECT
DATABASE();
+------------+
| DATABASE() |
+------------+
|
test |
+------------+
2.VERSION()函数:用户查询所使用数据库的版本
mysql>
? version
mysql> SELECT VERSION();
+------------+
| VERSION()
|
+------------+
| 5.0.22-log
|
+------------+
3.USER()函数:查询当前登陆用户名
mysql> SELECT
USER();
+----------------+
| USER() |
+----------------+
|
root@localhost
|
+----------------+
4.INET_ATON(expr)函数:查询IP地址的网络字节序表示,反函数是INET_NTOA
mysql>
SELECT INET_ATON('144.194.192.183');
+------------------------------+
|
INET_ATON('144.194.192.183')
|
+------------------------------+
| 2428682423
|
+------------------------------+
5.INET_NTOA(expr)函数:查询网络字节序代表的IP地址,是INET_ATON的反函数
mysql>
? inet_ntoa
mysql> SELECT
INET_NTOA(2428682423);
+-----------------------+
| INET_NTOA(2428682423)
|
+-----------------------+
| 144.194.192.183
|
+-----------------------+
六、小结
有事没事多请教一下帮助is a good idea.
也许这就是开源的好处,她会尽可能的考虑到您查询和参考的便利性。MySQL的函数还是很丰富的,以上实验用到的函数都是非常常用的。
这个小文儿,可以“一看了之”,只要你能想到MySQL提供的这些功能函数,check一下帮助系统,就什么都得到啦。
如果这些函数仍然没有满足您的求知欲望,敬请参考官方文档《第12章:函数和操作符》,地址为:http://dev.mysql.com/doc/refman/5.1/zh/functions.html