zoukankan      html  css  js  c++  java
  • SQL读取当天的数据

    SQL读取当天的数据

    1、SQL在查询当天记录时要注意是从当天的0点0分0秒0毫秒开始,到次日0点0分0秒0毫秒截止,但不包含次日的0点0分0秒0毫秒。

    2、注意:在不同数据库产品中,获得当天日期的函数不一样。
    MSSQL获得当前日期:

    convert(varchar(10),Getdate(),120)
    

    MYSQL获得当前日期:

    date(now())
    

    Oracle获得当前日期:

    to_char(sysdate,'yyyy-mm-dd')
    

    Access获得当前日期:

    date()
    

    3、在各个数据库里获得当天的记录写法为(假设表名为:Table_1,日期列名为:date_col):
    MSSQL获得当天记录:

    select * from table_1 where date_col>=convert(varchar(10),Getdate(),120) and date_col<convert(varchar(10),dateadd(d,1,Getdate()),120)
    

    MYSQL获得当天记录:

    select * from table_1 where date_col>=date(now()) and date_col<DATE_ADD(date(now()),INTERVAL 1 DAY)
    

    Oracle获得当天记录:

    select * from table_1 where date_col>=to_char(sysdate,'yyyy-mm-dd') and date_col<to_char(sysdate+1,'yyyy-mm-dd')
    

    Access获得当天记录:

    select * from table_1 where date_col>=date() and date_col<DateAdd("d",1,date())
    

    4、另外,在查询的时候,尽量不要对列进行运算,因为日期列上若有索引,就无法使用索引了。

  • 相关阅读:
    RabbitMQ:六、网络分区
    RabbitMQ:五、高阶
    RabbitMQ:四、跨越集群
    数据结构:红黑树
    RabbitMQ:三、进阶
    面对对象多态的异常
    面向对象三大特征---多态
    面对对象继承的优点和缺点
    面对对象当中的代码块
    面对对象this关键字
  • 原文地址:https://www.cnblogs.com/wjw1014/p/12680339.html
Copyright © 2011-2022 走看看