四、日期和时间函数
//返回当前的日期 curdate()或current_date() select curdate(); // 2014-12-05 select current_date() // 2014-12-05
//返回当前的时间 curtime()或current_time() select curtime() // 12:00:00 select current_time() // 12:00:00
//返回日期date加上间隔时间int的结果(int必须按照关键字进行格式化), date_add(date,interval int keyword) select date_add(current_date,interval 6 month);
//依照指定的fmt格式格式化日期date值 date_format(date,fmt) select date_format(curdate(),'%Y-%m-%d'); // 2014-12-05
//返回日期date加上间隔时间int的结果(int必须按照关键字进行格式化) date_sub(date,interval int keyword) select date_sub(current_date,interval 6 month);
// 返回date所代表的一星期中的第几天(1~7) dayofweek(date) select dayofweek('2014-12-5'); // 6
//返回date是一个月的第几天(1~31) dayofmonth(date) select dayofmonth('2014-12-5'); // 5
//返回date是一年的第几天(1~366) dayofyear(date) select dayofyear('2014-12-5') // 339
//返回date的星期名, dayname(date) select dayname(current_date); // Friday
//根据指定的fmt格式,格式化unix时间戳ts from_unixtime(ts,fmt) select from_unixtime(1417757025); // 2014-12-05 13:23:45 select from_unixtime(1417757025,'%Y-%m-%d %H:%i:%S'); //2014-12-05 13:23:45
//当前时间戳 unix_timestamp(); //utc时间 utc_timestamp();
//返回time的小时值(0~23) hour(time) select hour( '2014-05-05 23:11:22'); //22
//返回time的分钟值(0~59) minute(time) select minute('2014-05-05 23:11:22'); //11
//返回date的月份值(1~12) month(date) select month('2014-05-05 23:11:22'); //5 //返回date的月份名, monthname(date) select monthname('2014-05-05 23:11:22'); //May
//返回当前的日期和时间 now() || current_timestamp() select now() // 2014-12-05 12:00:00
//返回date在一年中的季度(1~4), quarter(date) select quarter(current_date); //4
//返回日期date为一年中第几周(0~53) week(date) select week(curdate()); //48
//返回日期date的年份(1000~9999) year(date) select year(current_date); // 2014 一些示例:
//返回1970年至今的天数 select to_days(now()) // 735935
//获取今天注册的用户 SELECT COUNT( uid ) sum FROM qb_memberdata WHERE to_days(FROM_UNIXTIME(时间戳)) = to_days(now())
//获取昨天注册的用户 SELECT COUNT( uid ) sum FROM qb_memberdata WHERE ( to_days(now())-to_days( '2014-12-04' ) ) = 1
//获取当前系统时间: select from_unixtime(unix_timestamp()); select extract(year_month from current_date); select extract(day_second from current_date); select extract(hour_minute from current_date);
//返回两个日期值之间的差值(月数):select period_diff(200302,199802); //在mysql中计算年龄: select date_format(from_days(to_days(now())-to_days(birthday)),'%y')+0 as age from employee; 这样,如果brithday是未来的年月日的话,计算结果为0。
//下面的sql语句计算员工的绝对年龄,即当birthday是未来的日期时,将得到负值。 select date_format(now(), '%y') - date_format(birthday, '%y') -(date_format(now(), '00-%m-%d') 100,'true','false'); if()函数在只有两种可能结果时才适合使用。然而,在现实世界中,我们可能发现在条件测试中会需要多个分支。在这种情况下,mysql提供了case函 数,它和php及perl语言的switch-case条件例程一样。 case函数的格式有些复杂,通常如下所示: case [expression to be evaluated] when [val 1] then [result 1] when [val 2] then [result 2] when [val 3] then [result 3] ...... when [val n] then [result n] else [default result] end //这里,第一个参数是要被判断的值或表达式,接下来的是一系列的when-then块,每一块的第一个参数指定要比较的值,如果为真,就返回结果。所有 的when-then块将以else块结束,当end结束了所有外部的case块时,如果前面的每一个块都不匹配就会返回else块指定的默认结果。如果 没有指定else块,而且所有的when-then比较都不是真,mysql将会返回null。 case函数还有另外一种句法,有时使用起来非常方便,如下: case when [conditional test 1] then [result 1] when [conditional test 2] then [result 2] else [default result] end 这种条件下,返回的结果取决于相应的条件测试是否为真。 示例: mysql> select case 'green' when 'red' then 'stop' when 'green' then 'go' end; select case 9 when 1 then 'a' when 2 then 'b' else 'n/a' end; select case when (2+2)=4 then 'ok' when(2+2)<>4 then 'not ok' end asstatus; select name,if((isactive = 1),'已激活','未激活') as result fromuserlogininfo; select fname,lname,(math+sci+lit) as total, case when (math+sci+lit) < 50 then 'd' when (math+sci+lit) between 50 and 150 then 'c' when (math+sci+lit) between 151 and 250 then 'b' else 'a' end as grade from marks; #一个登陆验证 select if(encrypt('sue','ts')=upass,'allow','deny') as loginresultfrom users where uname = 'sue';
七、格式化函数
//依照字符串fmt格式化日期date值 date_format(date,fmt)
//把x格式化为以逗号隔开的数字序列,y是结果的小数位数 format(x,y)
//返回ip地址的数字表示 inet_aton(ip)
//返回数字所代表的ip地址 inet_ntoa(num)
//依照字符串fmt格式化时间time值 time_format(time,fmt)
//其中最简单的是format()函数,它可以把大的数值格式化为以逗号间隔的易读的序列。
示例: select format(34234.34323432,3); select date_format(now(),'%w,%d %m %y %r'); select date_format(now(),'%y-%m-%d'); select date_format(19990330,'%y-%m-%d'); select date_format(now(),'%h:%i %p'); select inet_aton('10.122.89.47'); select inet_ntoa(175790383); 八、类型转化函数 //为了进行数据类型转化,mysql提供了cast()函数,它可以把一个值转化为指定的数据类型。类型 有:binary,char,date,time,datetime,signed,unsigned 示例: select cast(now() as signed integer),curdate()+0; select 'f'=binary 'f','f'=cast('f' as binary); 九、系统信息函数 //返回当前数据库名 database() //将表达式expr重复运行count次 benchmark(count,expr) //返回当前客户的连接id connection_id() //返回最后一个select查询进行检索的总行数 found_rows() //返回当前登陆用户名 user()或system_user() //返回mysql服务器的版本 version() 示例: select database(),version(),user(); select benchmark(9999999,log(rand()*pi())); #该例中,mysql计算log(rand()*pi())表达式9999999次。