9 sql 中對日期的處理

2021-09-02 18:16:29 字數 1134 閱讀 6907

1 查詢當天的使用量

select count(*)  count  from t_user where date(create_time)=(select curdate())
2查詢本週

select * from t_user where yearweek(date_format(create_time,'%y-%m-%d')) = yearweek(now());
·1、幾個小時內的資料

date_sub(now(), interval 5 hour)
·2、今天

select * from 表名 where to_days(時間欄位名) = to_days(now());
·3、昨天

select * from 表名 where to_days( now( ) ) - to_days( 時間欄位名) <= 1;
·4、7天

select * from 表名 where date_sub(curdate(), interval 7 day) <= date(時間欄位名);
·5、近30天

select * from 表名 where date_sub(curdate(), interval 30 day) <= date(時間欄位名);
·6、本月

select * from 表名 where date_format( 時間欄位名, '%y%m' ) = date_format( curdate( ) , '%y%m' );
·7、上一月

select * from 表名 where period_diff( date_format( now( ) , '%y%m' ) , date_format( 時間欄位名, '%y%m' ) ) =1;

(9)SQL的注入(致命的漏洞)

使用者登陸 的時候進行賬戶驗證輸入特殊的格式和字元會觸發乙個漏洞,不需要密碼直接登入成功 import pymysql username input 請輸入賬號 password input 請輸入密碼 conn pymysql.connect host localhost user root pa...

mysql資料庫對日期的篩選 sql中對日期的篩選

幾個小時內的資料 date sub now interval 5 hour 今天select from 表名 where to days 時間欄位名 to days now 昨天select from 表名 where to days now to days 時間欄位名 1 7天select fro...

Oracle 的TO DATE對日期處理

用例 select to date 2014 09 03 yyyy mm dd hh24 mi ss as nowtime from dual 1.日期和字元轉換函式用法 to date,to char select to char sysdate,yyyy mm dd hh24 mi ss as ...