zoukankan      html  css  js  c++  java
  • SQL查询月、天、周、年

    /**//*计算今天是星期几*/
    select datename(weekday,getdate())

    /**//*查询本年的数据*/
    select * from  users where year(time)=year(getdate())  

    /**//*查询本月的数据,time是表users中代表时间的字段*/
    select * from users where month(time)=month(getdate()) and year(time)=year(getdate())

    /**//*查询今天的数据,time 是表中代表时间的字段*/
    select * from users where day(time)=day(getdate()) and month(time)=month(getdate()) and year(time)=year(getdate())

    /**//*计算那一天是星期一*/
    SELECT  DATEADD(wk,  DATEDIFF(wk,0,getdate()),  0) 

    /**//*计算那一天是周末*/
    select dateadd(wk,datediff(wk,0,getdate()),6)

    /**//*查询本周的数据*/
    select * from users where DATEPART(wk, time) = DATEPART(wk, GETDATE()) and DATEPART(yy, time) = DATEPART(yy, GETDATE())

    /**//*查询本日的记录*/
    select * from users where (DATEDIFF(dd, time, GETDATE()) = 0)

    /**//*查询本月的记录*/
    select * from users where (DATEDIFF(mm, time, GETDATE()) = 0)

    /**//*查询本年的记录*/
    select * from users where (DATEDIFF(yy, time, GETDATE()) = 0)在MySql中实现:
       1——  
       本年:  
       select   *   from   loanInfo   where   year(date)=year(getdate())  
        
       2——  
       本月:  
       select   *   from   loanInfo   where   year(date)=year(getDate())   And   month(date)=month(getdate())  
        
       3——  
      本日:  
      select   *   from   loanInfo   where   year(date)=year(getDate())   And   month(date)=month(getdate())   and   Day(date)=Day(getDate()) 

    SELECT   *    FROM   table    WHERE   (MONTH(字段)   =   MONTH(GETDATE())) 

  • 相关阅读:
    数组顺序表
    指针、数组、结构体
    急救模式下安装rpm包
    如何杀死远程服务器到本机的tcp连接
    centos升级内核之后修改内核启动顺序
    rpm yum 等命令无响应的解决方法
    关于ssh 设置的相关总结(ssh最大连接数、ssh连接时长、安全性配置等)
    详解Linux中的日志及用日志来排查错误的方法
    linux 普通用户登陆系统su
    如何更新/升级Red Hat Enterprise Linux内核?
  • 原文地址:https://www.cnblogs.com/wubin264/p/1433719.html
Copyright © 2011-2022 走看看