mysql查询特定时间段内的数据

SET FOREIGN_KEY_CHECKS=0;



-- Table structure for t_user
-- ----------------------------
DROP TABLE IF EXISTS t_user;
CREATE TABLE t_user (
userId bigint(20) NOT NULL,
fullName varchar(64) NOT NULL,
userType varchar(16) NOT NULL,
addedTime datetime NOT NULL,
PRIMARY KEY (userId)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;



-- Records of t_user
-- ----------------------------
INSERT INTO t_user VALUES (‘1‘, ‘爽爽‘, ‘普通‘, ‘2018-01-21 10:20:09‘);
INSERT INTO t_user VALUES (‘2‘, ‘贵贵‘, ‘普通‘, ‘2017-11-06 10:20:22‘);
INSERT INTO t_user VALUES (‘3‘, ‘芬芬‘, ‘vip‘, ‘2017-11-13 10:20:42‘);
INSERT INTO t_user VALUES (‘4‘, ‘思思‘, ‘vip‘, ‘2018-01-21 10:20:55‘);
INSERT INTO t_user VALUES (‘5‘, ‘妍妍‘, ‘vip‘, ‘2017-09-17 10:21:28‘);

    下面是sql语句:

[sql] view plain copy
-- 今天
select fullName,addedTime from t_user where to_days(addedTime) = to_days(now());
-- 昨天
select fullName,addedTime from t_user where to_days(NOW()) - TO_DAYS(addedTime) <= 1;
-- 近7天
select fullName,addedTime from t_user where date_sub(CURDATE(),INTERVAL 7 DAY) <= DATE(addedTime);
-- 近30天
SELECT fullName,addedTime FROM t_user where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(addedTime);
-- 本月
SELECT fullName,addedTime FROM t_user WHERE DATE_FORMAT( addedTime, ‘%Y%m‘ ) = DATE_FORMAT( CURDATE() , ‘%Y%m‘ );
-- 上一月
SELECT fullName,addedTime FROM t_user WHERE PERIOD_DIFF( date_format( now( ) , ‘%Y%m‘ ) , date_format( addedTime, ‘%Y%m‘ ) ) =1;
-- 查询本季度数据
select fullName,addedTime FROM t_user where QUARTER(addedTime)=QUARTER(now());
-- 查询上季度数据
select fullName,addedTime FROM t_user where QUARTER(addedTime)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));
-- 查询本年数据
select fullName,addedTime FROM t_user where YEAR(addedTime)=YEAR(NOW());
-- 查询上年数据
select fullName,addedTime FROM t_user where year(addedTime)=year(date_sub(now(),interval 1 year));
-- 查询距离当前现在6个月的数据
select fullName,addedTime FROM t_user where addedTime between date_sub(now(),interval 6 month) and now();

-- 查询当前这周的数据
SELECT fullName,addedTime FROM t_user WHERE YEARWEEK(date_format(addedTime,‘%Y-%m-%d‘)) = YEARWEEK(now());
-- 查询上周的数据
SELECT fullName,addedTime FROM t_user WHERE YEARWEEK(date_format(addedTime,‘%Y-%m-%d‘)) = YEARWEEK(now())-1;
-- 查询上个月的数据
select fullName,addedTime FROM t_user where date_format(addedTime,‘%Y-%m‘)=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),‘%Y-%m‘);
-- 查询当前月份的数据
select fullName,addedTime FROM t_user where DATE_FORMAT(addedTime,‘%Y%m‘) = DATE_FORMAT(CURDATE(),‘%Y%m‘);
select fullName,addedTime FROM t_user where date_format(addedTime,‘%Y-%m‘)=date_format(now(),‘%Y-%m‘);

-- 查询指定时间段的数据
select fullName,addedTime FROM t_user where addedTime between ‘2017-1-1 00:00:00‘ and ‘2018-1-1 00:00:00‘;
select fullName,addedTime FROM t_user where addedTime >=‘2017-1-1 00:00:00‘ and addedTime < ‘2018-1-1 00:00:00‘;

    归纳一下:
    1、查询时间段内的数据,一般可以用between and 或 <> 来指定时间段。

    2、mysql的时间字段类型有:datetime,timestamp,date,time,year。

   3、 获取系统当前时间的函数:

    select CURDATE();
    select NOW();

    4、获取时间差的函数:

    period_diff()    datediff(date1,date2)      timediff(time1,time2)

   5、日期加减函数:

    date_sub() 

    date_add()     adddate()      addtime()

    period_add(P,N)    

    --------以上参考文章(mysql日期加减)

    6、时间格式转化函数:

    date_format(date, format) ,MySQL日期格式化函数date_format()
    unix_timestamp()
    str_to_date(str, format)
    from_unixtime(unix_timestamp, format) ,MySQL时间戳格式化函数from_unixtime

    --------以上参考文章(MYSQL日期 字符串 时间戳互转)

    顺带写一下oracle的查询语句:

[sql] view plain copy
select * from Oracle.alarmLog where alarmtime between to_date(‘2007-03-03 18:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘) and to_date(‘2007-09-04 18:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘)

另一篇博客

MySQL获取某个时间范围内的数据 TO_DAYS(date)函数
1、利用to_days函数查询今天的数据:

select * from 表名 where to_days(时间字段名) = to_days(now());

to_days函数:返回从0000年(公元1年)至当前日期的总天数。
2、昨天
SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) – TO_DAYS( 时间字段名) <= 1
3、7天
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(时间字段名)
4、近30天
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(时间字段名)
5、本月
SELECT * FROM 表名 WHERE DATE_FORMAT( 时间字段名, ‘%Y%m‘ ) = DATE_FORMAT( CURDATE( ) , ‘%Y%m‘ )
6、上一月
SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , ‘%Y%m‘ ) , date_format( 时间字段名, ‘%Y%m‘ ) ) =1

查询本季度数据

select * from ht_invoice_information where QUARTER(create_date)=QUARTER(now());

查询上季度数据

select * from ht_invoice_information where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));

查询本年数据

select * from ht_invoice_information where YEAR(create_date)=YEAR(NOW());

查询上年数据

select * from ht_invoice_information where year(create_date)=year(date_sub(now(),interval 1 year));

查询当前这周的数据
SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,‘%Y-%m-%d‘)) = YEARWEEK(now());

查询上周的数据
SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,‘%Y-%m-%d‘)) = YEARWEEK(now())-1;

查询当前月份的数据
select name,submittime from enterprise where date_format(submittime,‘%Y-%m‘)=date_format(now(),‘%Y-%m‘);

查询距离当前现在6个月的数据
select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();

查询上个月的数据
select name,submittime from enterprise where date_format(submittime,‘%Y-%m‘)=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),‘%Y-%m‘)
select * from user where DATE_FORMAT(pudate, ‘ %Y%m ‘ ) = DATE_FORMAT(CURDATE(), ‘ %Y%m ‘ ) ;
select * from user where WEEKOFYEAR(FROM_UNIXTIME(pudate,‘%y-%m-%d‘)) = WEEKOFYEAR(now())
select
from user
where MONTH (FROM_UNIXTIME(pudate, ‘ %y-%m-%d ‘ )) = MONTH (now())
select

from [ user ]
where YEAR (FROM_UNIXTIME(pudate, ‘ %y-%m-%d ‘ )) = YEAR (now())
and MONTH (FROM_UNIXTIME(pudate, ‘ %y-%m-%d ‘ )) = MONTH (now())
select
from [ user ]
where pudate between 上月最后一天
and 下月第一天
where date(regdate) = curdate();
select
from test where year(regdate)=year(now()) and month(regdate)=month(now()) and day(regdate)=day(now())
SELECT date( c_instime ) ,curdate( )
FROM t_score
WHERE 1
LIMIT 0 , 30

原文地址:https://www.cnblogs.com/plusUltra/p/10967072.html

时间: 2024-12-17 16:25:26

mysql查询特定时间段内的数据的相关文章

数据库SQL语句查询指定时间段内的数据

[摘要]有的时候,我们需要查询数据库某段时间之间的数据,比如2016年5月1号到到5月3号之间用户注册数量(特殊节假日期间)等.那么用SQL语句如何实现呢? 首先,数据表中的存时间的字段比如是addtime可能是时间戳(varchar),也可能是用timestamp类型表示,如果是时间戳格式当然好办,如果是timestamp格式:YYYY-MM-DD HH:II:SS这个格式,如2016-05-01 00:00:00这种格式的SQL语句怎么表示?有下面两张方式: (1)第一种方法,使用关键字be

MYSQL查询一周内的数据(最近7天的)

select * from wap_content where week(created_at) = week(now) 如果你要严格要求是某一年的,那可以这样 查询一天: select * from table where to_days(column_time) = to_days(now());select * from table where date(column_time) = curdate(); 查询一周: select * from table where DATE_SUB(C

Sqlite查询时间段内的数据问题解决!

最近搞Sqlite本地查询,需求为查询某时间段内的数据,在SQL中我们都知道为: [sql] view plaincopyprint? select * from tblName where rDate Between '2008-6-10' and  '2008-6-12' select * from tblName where rDate Between '2008-6-10' and '2008-6-12' 这样子是没有问题的,但是在Sqlite中我们这样子写是得不到结果的,试了好多次终于

MySQL 查询时间段内的数据

先来建表语句: SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for t_user -- ---------------------------- DROP TABLE IF EXISTS `t_user`; CREATE TABLE `t_user` ( `userId` bigint(20) NOT NULL, `fullName` varchar(64) NOT NULL, `use

(转)MySQL获取某个时间范围内的数据 TO_DAYS(date)函数与now()函数

有这样一个需求,我们需要获得某个时间段内所有数据记录,比如最近一个月内的新闻,然后再在这个数据集合中进行排序,看哪个新闻的点击率最高.现在问题是,如何获得那个时间段.MySQL已经有些函数可以帮助我们,先看看这些函数吧. TO_DAYS(date) 函数 给定一个日期date,返回一个天数(从年份0开始的天数). 比如下面的例子: 1 mysql> SELECT TO_DAYS('2009-08-07'); 2 +-----------------------+ 3 | TO_DAYS('200

Oracle查询某个时间段内第天的统计数

1:Oracle查询某个时间段内,每天发送短信的总条数 select tab.tday,       (select count(1) from t_msgsendrecode m where trunc(m.sendtime) = to_date(tab.tday,'yyyy-mm-dd'))from ( select * from (select to_char(add_months(last_day(to_date('2015-6-2','yyyy-mm-dd'))+1,-1),'YYYY

mysql 查询重复的(不区分大小写)数据的SQL优化

在mysql中查询不区分大小写重复的数据,往往会用到子查询,并在子查询中使用upper函数来将条件转化为大写.如: select * from staticcatalogue WHERE UPPER(Source) IN (SELECT UPPER(Source) FROM staticcatalogue GROUP BY UPPER(Source) having count(UPPER(Source))>1) ORDER BY upper(Source) DESC; 这条语句的执行效率是非常低

SQL查询某个时间段内的8-9点的数据

select datename(HOUR,rd.SignTime) 使用这个SQL函数就能查找到rd.SignTime的小时为多少 使用: SELECT * FROM table rd WHERE (select datename(HOUR,rd.SignTime))=8 则可以查询到  8:00:00-8:59:59的数据(就是 小时 为  8 的 时间)

ef linq 查询某时间段内数据 踩的坑

var now = DateTime.Now;var list =db.Jinbi_TypeLimit.Where(x => x.IsAvailable && x.JinbiType == jinbitype && x.StartDateTime <= now && now <= x.EndDateTime);//.ToList(); var list1 =db.Jinbi_TypeLimit.Where(x => x.IsAvail