数据库对日期进行比较

数据库对日期进行比较

原则是先把两个日期的格式统一一下,然后把日期字符串转化为日期,最后进行比较

转化为日期的两个时间可以做加减运算得出的结果为天数.

结果*24则得出的是小时数

结果*24*60得出的是分钟数

结果*24*60*60得出的是秒数

ceil((To_date(to_char(sysdate, 'yyyy-MM-dd HH24:mi:ss'),'yyyy-mm-dd hh24-mi-ss') -

            To_date(GXSJ, 'yyyy-mm-dd hh24-mi-ss')) * 24 * 60) > 10

计算两个日期相差的分钟数

ceil和floor函数在一些业务数据的时候,有时还是很有用的。

ceil(n) 取大于等于数值n的最小整数;

floor(n)取小于等于数值n的最大整数

select * from up_date where update < to_date('2007-09-07 00:00:00','yyyy-mm-dd hh24:mi:ss') select * from up_date where update <= to_date('2007-09-07 00:00:00','yyyy-mm-dd hh24:mi:ss')

在今天只后:

select * from up_date where update > to_date('2007-09-07 00:00:00','yyyy-mm-dd hh24:mi:ss') select * from up_date where update >= to_date('2007-09-07 00:00:00','yyyy-mm-dd hh24:mi:ss')

精确时间:

select * from up_date where update = to_date('2007-09-07 00:00:00','yyyy-mm-dd hh24:mi:ss')

在某段时间内:

select * from up_date where update between to_date('2007-07-07 00:00:00','yyyy-mm-dd hh24:mi:ss') and to_date('2007-09-07 00:00:00','yyyy-mm-dd hh24:mi:ss') select * from up_date where update < to_date('2007-09-07 00:00:00','yyyy-mm-dd hh24:mi:ss') and update > to_date('2007-07-07 00:00:00','yyyy-mm-dd hh24:mi:ss') select * from up_date where update <= to_date('2007-09-07 00:00:00','yyyy-mm-dd hh24:mi:ss') and update >= to_date('2007-07-07 00:00:00','yyyy-mm-dd hh24:mi:ss')

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);

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' )

mysql中多条件判断:

要求日期在2017-12-28,且city这一列的值为Beijing,SQL语句如下:

SELECT * FROM table_name where UNIX_TIMESTAMP(flightDate)=UNIX_TIMESTAMP('2017-12-28') and city='Beijing';

猜你喜欢

转载自blog.csdn.net/qq_34971162/article/details/81127977