HOME> 装扮工坊> 数据库查询当前时间大全(MySQL、Oracle、Sqlserver)
{$vo.文章标题}
{$vo.文章标题}

数据库查询当前时间大全(MySQL、Oracle、Sqlserver)

admin
9837

文章目录

前言数据库简介优点

MySql时间查询MySql查询当前时间MySql查询上一年、上一个月、上一个星期...MySql查询本年、本月、本周...

Oracle时间查询oracle查询当前时间Oracle查询上一年、上一个月、上一个星期...Oracle查询一年内、一个月内、一个星期内...

Sqlserver时间查询Sqlserver查询当前时间Sqlserver查询最近一周、最近一个月...SqlServer查询当前年、当前月、当前周...

前言

数据库简介

SqlServser:Microsoft产品,通过良好的ODBC接口可以将Access、FoxPro、Excel转换为SQLServer数据库。

Oracle:运行于所有主流平台,完全支持所有的工业标准(ODBC、JDBC、OCI),采用完全开放策略。Oracle并行服务器通过使一组结点共享同一簇中的工作扩展Windows NT的能力,提高可利用性和高伸缩性的簇的解决方案。支持大并发,大访问量。价格昂贵。

MySql:不支持事务处理,没有视图,没有存储过程和触发器,没有数据库端的用户自定义的函数,不能完全适用标准的SQL语法。目前可能已经支持。

优点

MySQL优点:体积小、速度快、总体拥有成本低,开放源码,搭配“L(Linux)A(Apache)M(MySQL)P(PHP/Perl/Python)“或“LN(Nginx)MP”就可以建立起一个稳定、免费的网站系统,适合中小型网站。 Oracle优点:使用方便、功能强大,可靠性好、安全性好、可移植性好、适应高吞吐量,适用于各类大、中、小、微机环境。 SqlServer优点:图形化用户界面,丰富的编程接口工具,与Windows NT完全集成,支持分布式的分区视图,适用于Win的Web技术的开发。

MySql时间查询

MySql查询当前时间

查询 年-月-日 时:分:秒 select now()查询 年-月-日 select DATE(CURDATE())查询 年-月 select date_format(NOW(),'%Y-%m')查询当前年 select YEAR(CURDATE())查询当前月 select MONTH(CURDATE())查询当前日 select DAYOFMONTH(NOW())查询当前星期几 select dayofweek(NOW()) -1查询当前季度 select QUARTER(now())查询 时:分:秒 select current_time()

查看结果

对应上面的序号展示输出的结果

最后给大家介绍一个万能的,可以单独取出年、月、日、时、分、秒 select DATE_FORMAT(now(),'%Y-%m-%d %h:%i:%s')

比如想取出当前的小时 select DATE_FORMAT(now(),'%h')

MySql查询上一年、上一个月、上一个星期…

查询昨天的数据 select * from test as 'time' where time.update_time

查询上一周的数据 select * from test as 'time' where time.update_time

查询上一个月的数据 select * from test as 'time' where time.update_time

查询上一年的数据 select * from test as 'time' where time.update_time

这些查询都是类似的,并且INTERVAL 1 函数还可以增加查询,比如:需要查询近六天的数据就可以将数字变成6就可以了 select * from test as 'time' where time.update_time

MySql查询本年、本月、本周…

查询今天的数据 select * from test as time where to_days(time.update_time ) =to_days(now());

查询本周的数据 select * FROM test as time WHERE YEARWEEK(date_format(time.update_time ,'%Y-%m-%d')) = YEARWEEK(now());

查询本月的数据 select * FROM test as time WHERE DATE_FORMAT(time.update_time , '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )

查询本季度的数据 select * from test as time where QUARTER(time.update_time)=QUARTER(now());

查询本年的数据 select * from test as time where YEAR(time.update_time)=YEAR(NOW());

Oracle时间查询

oracle查询当前时间

oracle数据库中有一个函数是和Mysql中的DATE_FORMAT函数差不多,下面主要来介绍他的使用方式:

查询当前时间 年-月-日 时:分:秒 select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual查询当前年 select to_char(sysdate,'yyyy') from dual查询当前月 select to_char(sysdate,'mm') from dual查询当前日 select to_char(sysdate,'dd ') from dual查询当前小时(按24小时) select to_char(sysdate,'hh24') from dual查询当前分钟 select to_char(sysdate,'mi') from dual查询当前秒 select to_char(sysdate,'ss') from dual查询当前季节 select to_char(sysdate,'q') from dual

注意:在Oracle 中,FROM 关键字是必不可少的 输出结果

Oracle查询上一年、上一个月、上一个星期…

获取10分钟前的日期 sql select sysdate,sysdate-interval '10' minute from dual;

获取一个小时前的日期 方式一:select sysdate,sysdate-interval '1' hour from dual; 方式二:select sysdate,sysdate-1/24 from dual;

获取一天前的日期 方式一:select sysdate, sysdate - interval '1' day from dual; 方式二:select sysdate, sysdate - 1 from dual;

获取一周前的日期 select sysdate, sysdate - interval '7' day from dual;

获取一个月前的日期 select sysdate,sysdate-interval '1' month from dual;

获取一年前的日期 select sysdate,sysdate-interval '1' year from dual;

Oracle查询一年内、一个月内、一个星期内…

获取10分钟内的数据

SELECT count(*) FROM TB WHERE 字段 BETWEEN sysdate-interval '10' minute AND SYSDATE;

获取一个小时内的数据

SELECT count(*) FROM TB WHERE 字段 BETWEEN sysdate-interval '1' hour AND SYSDATE;

获取一天内的数据

SELECT count(*) FROM TB WHERE 字段 BETWEEN SYSDATE-1 AND SYSDATE;

获取一周内的数据 方式一:select count(*) from TB where 字段 > sysdate - interval '7' day;

方式二:select count(*) from TB where 字段 > sysdate - 7;

获取一个月内的数据 方式一:select count(*) from TB where 字段 > add_months(sysdate,-1);

方式二:SELECT count(*) FROM TB WHERE 字段 BETWEEN sysdate-interval '1' month AND SYSDATE;

获取一年内的数据

select count(*) from TB WHERE 字段 BETWEEN sysdate-interval '1' year AND SYSDATE;

Sqlserver时间查询

Sqlserver查询当前时间

获取当前日期 年-月-日 时:分:秒 select GETDATE()

取时间的某个部分 select datepart(yy.getDate()) --年 select datepart(mm.getDate()) --月 select datepart(dd.getDate()) --日 select datepart(hh.getDate()) --时 select datepart(mi.getDate()) --分 select datepart(ss.getDate()) --秒

取当前星期几 select datepart(weekday.getDate()) --星期几

Sqlserver查询最近一周、最近一个月…

查询最近一周 select * from 表名 where DateDiff(dd,datetime类型字段,getdate())<=7

查询最近一个月 select * from 表名 where DateDiff(dd,datetime类型字段,getdate())<=30

查询昨天 select * from 表名 where DateDiff(dd,datetime类型字段,getdate())=1

SqlServer查询当前年、当前月、当前周…

查询当前年数据 select * from 表名 where DateDiff(yy,datetime类型字段,getdate())=0查询当前月数据 select * from 表名 where DateDiff(mm,datetime类型字段,getdate())=0查询当前周数据 select * from table where datediff(week,C_CALLTIME,getdate())=0查询当前季数据 select * from table where datediff(qq,C_CALLTIME,getdate())=0查询今天数据 select * from 表名 where DateDiff(dd,datetime类型字段,getdate())=0

梦想也许在今天无法实现,但重要的是,它在你心里。重要的是,你一直在努力,加油!!!