您的位置:首页 > 数据库 > MySQL

mysql日期比较

2013-09-12 15:44 232 查看
mysql日期比较语句

 

select * from student where '2012-02-27 00:00:00' < created_date and '2012-02-29 00:00:00' > created_date

 

select * from student where UNIX_TIMESTAMP('2012-02-27 00:00:00') < UNIX_TIMESTAMP(created_date) and UNIX_TIMESTAMP('2012-02-29 00:00:00') > UNIX_TIMESTAMP(created_date);

 www.2cto.com  

SELECT * FROM student WHERE (UNIX_TIMESTAMP(created_date) - UNIX_TIMESTAMP('2012-02-26 00:00:00') ) >= 0 AND (UNIX_TIMESTAMP(created_date) - UNIX_TIMESTAMP('2012-02-29 00:00:00') ) <= 0

 

MySql中时间比较的实现

unix_timestamp 函数可以接受一个参数,也可以不使用参数。它的返回值是一个无符号的整数。不使用参数,它返回自1970年1月1日0时0分0秒到现在所经过的秒数,如果使用参数,参数的类型为时间类型或者时间类型的字符串表示,则是从1970-01-01 00:00:00到指定时间所经历的秒数。

有了这个函数,就可以很自然地把时间比较转换为一个无符号整数的比较。

例如,判断一个时间是否在一个区间内

unix_timestamp( time ) between unix_timestamp( 'start ') and unix_timestamp( 'end' )

函数:FROM_UNIXTIME
作用:将MYSQL中以INT(11)存储的时间以"YYYY-MM-DD"格式来显示。
语法:FROM_UNIXTIME(unix_timestamp,format)

SELECT FROM_UNIXTIME(1234567890, '%Y-%m-%d %H:%i:%S')


SELECT *,FROM_UNIXTIME(created, '%Y-%m-%d') as riqi FROM `rc_ms_users`
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  timestamp unix MySQL