mysql常用函数 二
结合mysql自带的帮助文档列一下mysql数据库中常用的一些函数。
事实证明:mysql的联机帮助资料非常实用,希望哪一天可爱的oracle可以像mysql学习一下,她可以让您基本不用查看其他的资料就将函数的基本使用方法和示例尽收眼底。
废话少说,直入主题
一、常用字符串函数
二、数值函数
三、日期和时间函数
四、mysql控制流程函数
1.if(expr1,expr2,expr3)函数:如果expr1为真则返回expr2,否则返回expr3
2.ifnull(expr1,expr2)函数:若expr1为null则返回expr2内容
3.nullif(expr1,expr2)函数:若expr1 = expr2则返回null,否则返回expr1
4.case函数
?
五、其他常用函数
时间格式:或者将里面的和java对比的将答谢的换程小写。
?根据format字符串格式化date值。下列修饰符可以被用在format字符串中: %m 月名字(january……december)?
%w 星期名字(sunday……saturday)?
%d 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)?
%y 年, 数字, 4 位?
%y 年, 数字, 2 位?
%a 缩写的星期名字(sun……sat)?
%d 月份中的天数, 数字(00……31)?
%e 月份中的天数, 数字(0……31)?
%m 月, 数字(01……12)?
%c 月, 数字(1……12)?
%b 缩写的月份名字(jan……dec)?
%j 一年中的天数(001……366)?
%h 小时(00……23)?
%k 小时(0……23)?
%h 小时(01……12)?
%i 小时(01……12)?
%l 小时(1……12)?
%i 分钟, 数字(00……59)?
%r 时间,12 小时(hh:mm:ss [ap]m)?
%t 时间,24 小时(hh:mm:ss)?
%s 秒(00……59)?
%s 秒(00……59)?
%p am或pm?
%w 一个星期中的天数(0=sunday ……6=saturday )?
%u 星期(0……52), 这里星期天是星期的第一天?
%u 星期(0……52), 这里星期一是星期的第一天?
%% 一个文字“%”。
所有的其他字符不做解释被复制到结果中。
mysql> select date_format('1997-10-04 22:23:00', '%w %m %y');?
-> 'saturday october 1997'?
mysql> select date_format('1997-10-04 22:23:00', '%h:%i:%s');?
-> '22:23:00'?
mysql> select date_format('1997-10-04 22:23:00',?
'%d %y %a %d %m %b %j');?
-> '4th 97 sat 04 10 oct 277'?
mysql> select date_format('1997-10-04 22:23:00',?
'%h %k %i %r %t %s %w');?
-> '22 22 10 10:23:00 pm 22:23:00 00 6'
一、常用字符串函数
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 +-----------------------------------------------------+
| case when salary +-----------------------------------------------------+
| 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
来源:http://my.oschina.net/taisha/blog/63451
?
?
