mysql 按年度、季度、月度、周、日SQL统计查询

  1. 一、年度查询  
  2.   
  3. 查询 本年度的数据  
  4.   
  5. SELECT *  
  6.   
  7. FROM blog_article  
  8.   
  9. WHERE year( FROM_UNIXTIME( BlogCreateTime ) ) = year( curdate( ))  
  10.   
  11. 二、查询季度数据  
  12.   
  13. 查询数据附带季度数  
  14.   
  15. SELECT ArticleId, quarter( FROM_UNIXTIME( `BlogCreateTime` ) )  
  16.   
  17. FROM `blog_article`  
  18.   
  19. 其他的同前面部分:查询 本季度的数据  
  20.   
  21. SELECT *  
  22.   
  23. FROM blog_article  
  24.   
  25. WHERE quarter( FROM_UNIXTIME( BlogCreateTime ) ) = quarter( curdate( ))  
  26.   
  27. 三、查询月度数据  
  28.   
  29. 本月统计(MySQL)  
  30.   
  31. select * from booking where month(booking_time) =  
  32.   
  33. month(curdate()) and year(booking_time) = year(curdate())  
  34.   
  35. 本周统计(MySQL)  
  36.   
  37. select * from spf_booking where month(booking_time) =  
  38.   
  39. month(curdate()) and week(booking_time) = week(curdate())  
  40.   
  41. 四、时间段  
  42.   
  43. N天内记录  
  44.   
  45. WHERE TO_DAYS(NOW()) - TO_DAYS(时间字段) <= N  
  46.   
  47. 当天的记录  
  48.   
  49. where date(时间字段)=date(now())  
  50.   
  51. 或  
  52.   
  53. where to_days(时间字段) = to_days(now());  
  54.   
  55. 查询一周:  
  56.   
  57. select * from table   where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(column_time);  
  58.   
  59. 查询一个月:  
  60.   
  61. select * from table where DATE_SUB(CURDATE(), INTERVAL INTERVAL 1 MONTH) <= date(column_time);  
  62.   
  63. 查询'06-03''07-08'这个时间段内所有过生日的会员:  
  64.   
  65.    Select * From user Where  
  66.   
  67. DATE_FORMAT(birthday,'%m-%d') >= '06-03' and DATE_FORMAT(birthday,'%m-%d')  
  68.   
  69. <= '07-08';  
  70.   
  71. 统计一季度数据,表时间字段为:savetime  
  72.   
  73. group by concat(date_format(savetime, '%Y '),FLOOR((date_format(savetime, '%m ')+2)/3))  
  74.   
  75. 或  
  76.   
  77. select YEAR(savetime)*10+((MONTH(savetime)-1) DIV 3) +1,count(*)  
  78.   
  79. from yourTable  
  80.   
  81. group by YEAR(savetime)*10+((MONTH(savetime)-1) DIV 3) +1;  
  82.   
  83. 五、分组查询  
  84.   
  85.       
  86.   
  87.    1、年度分组  
  88.   
  89.    2、月度分组  
  90.   
  91.    3、先按年度分组,再按月度分组  
  92.   
  93.    4、按年月分组  
  94.   
  95.    SELECT count(ArticleId), date_format(FROM_UNIXTIME( `BlogCreateTime`),'%y%m') sdate  FROM `blog_article` group by sdate  
  96.   
  97.    结果:  
  98.   
  99.     count( ArticleId )     sdate  
  100.   
  101. 17     0901  
  102.   
  103. 11     0902  
  104.   
  105. 5     0903  
  106.   
  107. 6     0904  
  108.   
  109. 2     0905  
  110.   
  111. 1     0907  
  112.   
  113. 12     0908  
  114.   
  115. 6     0909  
  116.   
  117. 11     0910  
  118.   
  119. 3     0911  
  120.    
  121.    
  122. 其他方法参考:  
  123. 我想做一个统计,数据库是mysql,统计出每天,每周,每月的记录数  
  124. 建表的时候加个字段表示日期,然后查sql手册...  
  125. select count(*) from `tablewhere `date`='{某天}'  
  126. select count(*) from `tablewhere date_format(`date`,'%V')='{某周}'  
  127. select count(*) from `tablewhere date_format(`date`,'%c')='{某月}'  
  128. 另一种方法:  
  129. select count( * ) from projects where editdate >= '2007-11-9 00:00:00' and editdate <=  
  130. '2007-11-9 24:00:00';  
  131. 第三种方法:  
  132. 每周的  
  133. SQL codeselect count(*) as cnt,week(editdate) as weekflg from projects where year(editdate)  
  134. =2007 group by weekflg  
  135.   
  136. 每月  
  137. SQL codeselect count(*) as cnt,month(editdate) as monthflg from projects where year  
  138. (editdate)=2007 group by monthflg  
  139.   
  140. 每天  
  141. SQL codeselect count(*) as cnt from projects group by date(editdate)  
  142.    
  143.    
  144. mysql中DATE_FORMAT(date, format)函数可根据format字符串格式化日期或日期和时间值date,返回结果  
  145. 串。  
  146. 也可用DATE_FORMAT( ) 来格式化DATE 或DATETIME 值,以便得到所希望的格式。根据format字符串格式  
  147. date值:  
  148. 下面是函数的参数说明:  
  149. %S, %s 两位数字形式的秒( 00,01, . . ., 59)  
  150. %i 两位数字形式的分( 00,01, . . ., 59)  
  151. %H 两位数字形式的小时,24 小时(00,01, . . ., 23)  
  152. %h, %I 两位数字形式的小时,12 小时(01,02, . . ., 12)  
  153. %k 数字形式的小时,24 小时(0,1, . . ., 23)  
  154. %l 数字形式的小时,12 小时(1, 2, . . ., 12)  
  155. %T 24 小时的时间形式(hh : mm : s s)  
  156. %r 12 小时的时间形式(hh:mm:ss AM 或hh:mm:ss PM)  
  157. %p AM 或P M  
  158. %W 一周中每一天的名称( Sunday, Monday, . . ., Saturday)  
  159. %a 一周中每一天名称的缩写( Sun, Mon, . . ., Sat)  
  160. %d 两位数字表示月中的天数( 00, 01, . . ., 31)  
  161. %e 数字形式表示月中的天数( 1, 2, . . ., 31)  
  162. %D 英文后缀表示月中的天数( 1st, 2nd, 3rd, . . .)  
  163. %w 以数字形式表示周中的天数( 0 = Sunday, 1=Monday, . . ., 6=Saturday)  
  164. %j 以三位数字表示年中的天数( 001, 002, . . ., 366)  
  165. % U 周(0, 1, 52),其中Sunday 为周中的第一天  
  166. %u 周(0, 1, 52),其中Monday 为周中的第一天  
  167. %M 月名(January, February, . . ., December)  
  168. %b 缩写的月名( January, February, . . ., December)  
  169. %m 两位数字表示的月份( 01, 02, . . ., 12)  
  170. %c 数字表示的月份( 1, 2, . . ., 12)  
  171. %Y 四位数字表示的年份  
  172. %y 两位数字表示的年份  
  173. %% 直接值“%”  

猜你喜欢

转载自blog.csdn.net/Drug_/article/details/80499018