mysql查詢時間段內資料
先來:mysql查詢時間段內資料
後到:mysql查詢時間段內資料
1、建表語句:
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');
2、sql語句:
-- 今天
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';
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...
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 ...