mysql查詢特定時間段內的資料

2021-08-29 04:36:07 字數 4118 閱讀 2031

建表語句:

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

-- 今天  

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)

6、時間格式轉化函式:

date_format(date, format) ,mysql日期格式化函式date_format()

unix_timestamp()

str_to_date(str, format)

from_unixtime(unix_timestamp, format) ,mysql時間戳格式化函式from_unixtime

linux下查詢特定時間段內生成的檔案

如下,查詢9點到11點生成的檔案,不包括9和11,即查詢10點生成的檔案 方法1 ls l 20120816bak awk 方法2 先touch 2個時間段的時間標記touch t 08160900 starttime touch t 08161100 endtime然後 find 20120816...

mysql查詢時間段內資料

今天 select from 表名 where to days 時間欄位名 to days now 昨天select from 表名 where to days now to days 時間欄位名 1 近7天select from 表名 where date sub curdate interval...

mysql查詢時間段內資料

今天 select from 表名稱 where to days 建立時間 字段 to days now 昨天 select from 表名稱 where to days now to days 建立時間 字段 1 近七天 select from 表名稱 where date sub curdate...