mysql中有用的幾種統計sql

2021-08-28 13:04:33 字數 2456 閱讀 8507

**:親測通過

今天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 7 day) <= date(時間欄位名)

近30天

select * from 表名 where date_sub(curdate(), interval 30 day) <= date(時間欄位名)

本月select * from 表名 where date_format( 時間欄位名, 『%y%m』 ) = date_format( curdate( ) , 『%y%m』 )

上一月select * from 表名 where period_diff( date_format( now( ) , 『%y%m』 ) , date_format( 時間欄位名, 『%y%m』 ) ) =1

查詢本季度資料

select * fromht_invoice_informationwhere quarter(create_date)=quarter(now());

查詢上季度資料

select * fromht_invoice_informationwhere quarter(create_date)=quarter(date_sub(now(),interval 1 quarter));

查詢本年資料

select * fromht_invoice_informationwhere year(create_date)=year(now());

查詢上年資料

select * fromht_invoice_informationwhere 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(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 下月第一天

複製**

複製**

查詢當前月份的資料

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

查詢某個月的資料(查詢17年10月份資料)

select * from exam where date_format(starttime,』%y-%m』)=『2017-10』

select * from exam where date_format(starttime,』%y-%m』)=date_format(『2017-10-05』,』%y-%m』)

mysql 複製表的幾種方式以及常用的sql語句

mysql拷貝表操作我們會常常用到,下面就為您詳細介紹幾種mysql拷貝表的方式,希望對您學習mysql拷貝表方面能夠有所幫助。假如我們有以下這樣乙個表 id username password 1 admin 2 sameer 3 stewart create table if not exist...

mysql 複製表的幾種方式以及常用的sql語句

mysql拷貝表操作我們會常常用到,下面就為您詳細介紹幾種mysql拷貝表的方式,希望對您學習mysql拷貝表方面能夠有所幫助。假如我們有以下這樣乙個表 id username password 1 admin 2 sameer 3 stewart create table if not exist...

EditPlus中有用的快捷鍵

editplus本身就很強大,我第一次寫指令碼就是用的它。這很強大,但需要去學習靈活的使用它,下面舉幾個很實用,但往往會被忽略的快捷鍵 1 ctrl 搜尋匹配的 html 標籤 我要查詢第乙個div的結尾標記如何查詢?點選到該標記 全選或游標落在裡面即可 按下ctrl 就可以找到匹配的標籤了。權支援...