mysql查询特定时间数据视频_mysql查询特定时间段内的数据
參照文章(mysql查詢時間段內數據)進行了操作。
先來建表語句:
[sql]?view plaincopySETFOREIGN_KEY_CHECKS=0;--?------------------------------?Table?structure?for?t_user--?----------------------------DROPTABLEIF?EXISTS?`t_user`;CREATETABLE`t_user`?(`userId`?bigint(20)NOTNULL,`fullName`?varchar(64)NOTNULL,`userType`?varchar(16)NOTNULL,`addedTime`?datetime?NOTNULL,PRIMARYKEY(`userId`))?ENGINE=InnoDB?DEFAULTCHARSET=utf8;--?------------------------------?Records?of?t_user--?----------------------------INSERTINTO`t_user`VALUES('1','爽爽','普通','2018-01-21?10:20:09');INSERTINTO`t_user`VALUES('2','貴貴','普通','2017-11-06?10:20:22');INSERTINTO`t_user`VALUES('3','芬芬','vip','2017-11-13?10:20:42');INSERTINTO`t_user`VALUES('4','思思','vip','2018-01-21?10:20:55');INSERTINTO`t_user`VALUES('5','妍妍','vip','2017-09-17?10:21:28');下面是sql語句:
[sql]?view plaincopy--?今天selectfullName,addedTimefromt_userwhereto_days(addedTime) =?to_days(now());--?昨天selectfullName,addedTimefromt_userwhereto_days(NOW())?-?TO_DAYS(addedTime)?<=?1;--?近7天selectfullName,addedTimefromt_userwheredate_sub(CURDATE(),INTERVAL?7DAY)?<=DATE(addedTime);--?近30天SELECTfullName,addedTimeFROMt_userwhereDATE_SUB(CURDATE(),?INTERVAL?30DAY)?<=date(addedTime);--?本月SELECTfullName,addedTimeFROMt_userWHEREDATE_FORMAT(?addedTime,'%Y%m')?=?DATE_FORMAT(?CURDATE()?,'%Y%m');--?上一月SELECTfullName,addedTimeFROMt_userWHEREPERIOD_DIFF(?date_format(?now(?)?,'%Y%m')?,?date_format(?addedTime,'%Y%m')?)?=1;--?查詢本季度數據selectfullName,addedTimeFROMt_userwhereQUARTER(addedTime)=QUARTER(now());--?查詢上季度數據selectfullName,addedTimeFROMt_userwhereQUARTER(addedTime)=QUARTER(DATE_SUB(now(),interval?1?QUARTER));--?查詢本年數據selectfullName,addedTimeFROMt_userwhereYEAR(addedTime)=YEAR(NOW());--?查詢上年數據selectfullName,addedTimeFROMt_userwhereyear(addedTime)=year(date_sub(now(),interval?1year));--?查詢距離當前現在6個月的數據selectfullName,addedTimeFROMt_userwhereaddedTimebetweendate_sub(now(),interval?6month)andnow();--?查詢當前這周的數據SELECTfullName,addedTimeFROMt_userWHEREYEARWEEK(date_format(addedTime,'%Y-%m-%d'))?=?YEARWEEK(now());--?查詢上周的數據SELECTfullName,addedTimeFROMt_userWHEREYEARWEEK(date_format(addedTime,'%Y-%m-%d'))?=?YEARWEEK(now())-1;--?查詢上個月的數據selectfullName,addedTimeFROMt_userwheredate_format(addedTime,'%Y-%m')=date_format(DATE_SUB(curdate(),?INTERVAL?1MONTH),'%Y-%m');--?查詢當前月份的數據selectfullName,addedTimeFROMt_userwhereDATE_FORMAT(addedTime,'%Y%m')?=?DATE_FORMAT(CURDATE(),'%Y%m');selectfullName,addedTimeFROMt_userwheredate_format(addedTime,'%Y-%m')=date_format(now(),'%Y-%m');--?查詢指定時間段的數據selectfullName,addedTimeFROMt_userwhereaddedTimebetween'2017-1-1?00:00:00'and'2018-1-1?00:00:00';selectfullName,addedTimeFROMt_userwhereaddedTime?>='2017-1-1?00:00:00'andaddedTime?
1、查詢時間段內的數據,一般可以用between and 或 <> 來指定時間段。
2、mysql的時間字段類型有:datetime,timestamp,date,time,year。
3、 獲取系統(tǒng)當前時間的函數:
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 plaincopyselect*fromOracle.alarmLogwherealarmtimebetweento_date('2007-03-03?18:00:00','yyyy-mm-dd?hh24:mi:ss')andto_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
總結
以上是生活随笔為你收集整理的mysql查询特定时间数据视频_mysql查询特定时间段内的数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 洛阳钼业有哪些金属 拥有完整产业链条
- 下一篇: 神下款逾期会怎样