日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql日期格式化季度_mysql按年度、季度、月度、周、日统计查询的sql语句

發布時間:2023/12/10 数据库 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql日期格式化季度_mysql按年度、季度、月度、周、日统计查询的sql语句 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文介紹一些mysql中用于查詢的sql語句,包括按年度、季度、月度、周、日統計查詢等,有需要的朋友,可以參考下。

一、年度查詢

查詢 本年度的數據

SELECT *

FROM blog_article

WHERE year( FROM_UNIXTIME( BlogCreateTime ) ) = year( curdate( ))

二、查詢季度數據

查詢數據附帶季度數

SELECT ArticleId, quarter( FROM_UNIXTIME( `BlogCreateTime` ) )

FROM `blog_article`

查詢 本季度的數據

SELECT *

FROM blog_article

WHERE quarter( FROM_UNIXTIME( BlogCreateTime ) ) = quarter( curdate( ))

三、查詢月度數據

本月統計(MySQL)

select * from booking where month(booking_time) =

month(curdate()) and year(booking_time) = year(curdate())

本周統計(MySQL)

select * from spf_booking where month(booking_time) =

month(curdate()) and week(booking_time) = week(curdate())

四、時間段

N天內記錄

WHERE TO_DAYS(NOW()) - TO_DAYS(時間字段) <= N

當天的記錄

where date(時間字段)=date(now())

---或

where to_days(時間字段) = to_days(now());

查詢一周:

select * from table ? where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(column_time);

查詢一個月:

select * from table where DATE_SUB(CURDATE(), INTERVAL INTERVAL 1 MONTH) <= date(column_time);

查詢'06-03'到'07-08'這個時間段內所有過生日的會員:

Select * From user Where

DATE_FORMAT(birthday,'%m-%d') >= '06-03' and DATE_FORMAT(birthday,'%m-%d')

<= '07-08';

統計一季度數據,表時間字段為:savetime

group by concat(date_format(savetime, '%Y '),FLOOR((date_format(savetime, '%m ')+2)/3))

---或

select YEAR(savetime)*10+((MONTH(savetime)-1) DIV 3) +1,count(*)

from yourTable

group by YEAR(savetime)*10+((MONTH(savetime)-1) DIV 3) +1;

五、分組查詢

1、年度分組

2、月度分組

3、先按年度分組,再按月度分組

4、按年月分組

SELECT count(ArticleId), date_format(FROM_UNIXTIME( `BlogCreateTime`),'%y%m') sdate ?FROM `blog_article` group by sdate

結果:

count( ArticleId ) ? ? sdate

17 ? ? 0901

11 ? ? 0902

5 ? ? ?0903

6 ? ? 0904

2 ? ? 0905

1 ? ? 0907

12 ? ? 0908

6 ? ? 0909

11 ? ? 0910

3 ? ? 0911

其他方法參考:

做一個統計,數據庫是mysql,統計出每天,每周,每月的記錄數

建表的時候加個字段表示日期。

方法1,

select count(*) from `table` where `date`='{某天}'

select count(*) from `table` where date_format(`date`,'%V')='{某周}'

select count(*) from `table` where date_format(`date`,'%c')='{某月}'

方法2,

select count( * ) from projects where editdate >= '2007-11-9 00:00:00' and editdate <=

'2007-11-9 24:00:00';

方法3,

每周的

select count(*) as cnt,week(editdate) as weekflg from projects where year(editdate)

=2007 group by weekflg

每月

select count(*) as cnt,month(editdate) as monthflg from projects where year

(editdate)=2007 group by monthflg

每天

select count(*) as cnt from projects group by date(editdate)

mysql中DATE_FORMAT(date, format)函數可根據format字符串格式化日期或日期和時間值date,返回結果串。

也可用DATE_FORMAT( ) 來格式化DATE 或DATETIME 值,以便得到所希望的格式。根據format字符串格式化date值:

函數的參數說明:

%S, %s 兩位數字形式的秒( 00,01, . . ., 59)

%i 兩位數字形式的分( 00,01, . . ., 59)

%H 兩位數字形式的小時,24 小時(00,01, . . ., 23)

%h, %I 兩位數字形式的小時,12 小時(01,02, . . ., 12)

%k 數字形式的小時,24 小時(0,1, . . ., 23)

%l 數字形式的小時,12 小時(1, 2, . . ., 12)

%T 24 小時的時間形式(hh : mm : s s)

%r 12 小時的時間形式(hh:mm:ss AM 或hh:mm:ss PM)

%p AM 或P M

%W 一周中每一天的名稱( Sunday, Monday, . . ., Saturday)

%a 一周中每一天名稱的縮寫( Sun, Mon, . . ., Sat)

%d 兩位數字表示月中的天數( 00, 01, . . ., 31)

%e 數字形式表示月中的天數( 1, 2, . . ., 31)

%D 英文后綴表示月中的天數( 1st, 2nd, 3rd, . . .)

%w 以數字形式表示周中的天數( 0 = Sunday, 1=Monday, . . ., 6=Saturday)

%j 以三位數字表示年中的天數( 001, 002, . . ., 366)

% U 周(0, 1, 52),其中Sunday 為周中的第一天

%u 周(0, 1, 52),其中Monday 為周中的第一天

%M 月名(January, February, . . ., December)

%b 縮寫的月名( January, February, . . ., December)

%m 兩位數字表示的月份( 01, 02, . . ., 12)

%c 數字表示的月份( 1, 2, . . ., 12)

%Y 四位數字表示的年份

%y 兩位數字表示的年份

%% 直接值“%”若轉載請注明出處!若有疑問,請回復交流!

總結

以上是生活随笔為你收集整理的mysql日期格式化季度_mysql按年度、季度、月度、周、日统计查询的sql语句的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。