MySQL统计函数记录——按⽉、按季度、按⽇、时间段统计以及MySQL⽇期时间
函数⼤全
DAYOFWEEK(date)数据库管理系统有哪些mongodb
 返回⽇期date是星期⼏(1=星期天,2=星期⼀,……7=星期六,ODBC标准)
mysql> select DAYOFWEEK('1998-02-03');
  -> 3
WEEKDAY(date)
 返回⽇期date是星期⼏(0=星期⼀,1=星期⼆,……6= 星期天)。
mysql> select WEEKDAY('1997-10-04 22:23:00');
  -> 5
mysql> select WEEKDAY('1997-11-05');
  -> 2
DAYOFMONTH(date)
 返回date是⼀⽉中的第⼏⽇(在1到31范围内)
mysql> select DAYOFMONTH('1998-02-03');
  -> 3
DAYOFYEAR(date)
 返回date是⼀年中的第⼏⽇(在1到366范围内)
mysql> select DAYOFYEAR('1998-02-03');
  -> 34
MONTH(date)
 返回date中的⽉份数值
mysql> select MONTH('1998-02-03');
  -> 2
DAYNAME(date)
 返回date是星期⼏(按英⽂名返回)
mysql> select DAYNAME("1998-02-05");
  -> 'Thursday'
MONTHNAME(date)
 返回date是⼏⽉(按英⽂名返回)
mysql> select MONTHNAME("1998-02-05");
  -> 'February'
QUARTER(date)
 返回date是⼀年的第⼏个季度
mysql> select QUARTER('98-04-01');
  -> 2
WEEK(date,first)
 返回date是⼀年的第⼏周(first默认值0,first取值1表⽰周⼀是周的开始,0从周⽇开始)
mysql> select WEEK('1998-02-20');
  -> 7
mysql> select WEEK('1998-02-20',0);
  -> 7
mysql> select WEEK('1998-02-20',1);
  -> 8
YEAR(date)
 返回date的年份(范围在1000到9999)
mysql> select YEAR('98-02-03');
  -> 1998
HOUR(time)
 返回time的⼩时数(范围是0到23)
mysql> select HOUR('10:05:03');
  -> 10
MINUTE(time)
 返回time的分钟数(范围是0到59)
mysql> select MINUTE('98-02-03 10:05:03');
  -> 5
SECOND(time)
 返回time的秒数(范围是0到59)
mysql> select SECOND('10:05:03');
  -> 3
PERIOD_ADD(P,N)
 增加N个⽉到时期P并返回(P的格式YYMM或YYYYMM)
mysql> select PERIOD_ADD(9801,2);
  -> 199803
PERIOD_DIFF(P1,P2)
 返回在时期P1和P2之间⽉数(P1和P2的格式YYMM或YYYYMM)
mysql> select PERIOD_DIFF(9802,199703);
  -> 11
DATE_ADD(date,INTERVAL expr type)
DATE_SUB(date,INTERVAL expr type)
ADDDATE(date,INTERVAL expr type)
SUBDATE(date,INTERVAL expr type)
 对⽇期时间进⾏加减法运算
 (ADDDATE()和SUBDATE()是DATE_ADD()和DATE_SUB()的同义词,也可以⽤运算符和-⽽不是函数
 date是⼀个DATETIME或DATE值,expr对date进⾏加减法的⼀个表达式字符串type指明表达式expr应该如何被解释
 [type值含义期望的expr格式]:
 SECOND 秒 SECONDS
 MINUTE 分钟 MINUTES
 HOUR 时间 HOURS
 DAY 天 DAYS
 MONTH ⽉ MONTHS
 YEAR 年 YEARS
 MINUTE_SECOND 分钟和秒 "MINUTES:SECONDS"
 HOUR_MINUTE ⼩时和分钟 "HOURS:MINUTES"
 DAY_HOUR 天和⼩时 "DAYS HOURS"
 YEAR_MONTH 年和⽉ "YEARS-MONTHS"
 HOUR_SECOND ⼩时, 分钟, "HOURS:MINUTES:SECONDS"
 DAY_MINUTE 天, ⼩时, 分钟 "DAYS HOURS:MINUTES"
 DAY_SECOND 天, ⼩时, 分钟, 秒 "DAYS HOURS:MINUTES:SECONDS"
 expr中允许任何标点做分隔符,如果所有是DATE值时结果是⼀个DATE值,否则结果是⼀个DATETIME值) 如果type关键词不完整,则MySQL从右端取值,DAY_SECOND因为缺少⼩时分钟等于MINUTE_SECOND) 如果增加MONTH、YEAR_MONTH或YEAR,天数⼤于结果⽉份的最⼤天数则使⽤最⼤天数)
mysql> SELECT "1997-12-31 23:59:59" INTERVAL 1 SECOND;
  -> 1998-01-01 00:00:00
mysql> SELECT INTERVAL 1 DAY "1997-12-31";
  -> 1998-01-01
mysql> SELECT "1998-01-01" - INTERVAL 1 SECOND;
  -> 1997-12-31 23:59:59
mysql> SELECT DATE_ADD("1997-12-31 23:59:59",INTERVAL 1 SECOND);
  -> 1998-01-01 00:00:00
onclick啥意思mysql> SELECT DATE_ADD("1997-12-31 23:59:59",INTERVAL 1 DAY);
  -> 1998-01-01 23:59:59
mysql> SELECT DATE_ADD("1997-12-31 23:59:59",INTERVAL "1:1" MINUTE_SECOND);
  -> 1998-01-01 00:01:00
mysql> SELECT DATE_SUB("1998-01-01 00:00:00",INTERVAL "1 1:1:1" DAY_SECOND);
  -> 1997-12-30 22:58:59
mysql> SELECT DATE_ADD("1998-01-01 00:00:00", INTERVAL "-1 10" DAY_HOUR);
  -> 1997-12-30 14:00:00
mysql> SELECT DATE_SUB("1998-01-02", INTERVAL 31 DAY);
  -> 1997-12-02
mysql> SELECT EXTRACT(YEAR FROM "1999-07-02");
  -> 1999v站margin模型
mysql> SELECT EXTRACT(YEAR_MONTH FROM "1999-07-02 01:02:03");
  -> 199907
mysql> SELECT EXTRACT(DAY_MINUTE FROM "1999-07-02 01:02:03");
  -> 20102
TO_DAYS(date)
 返回⽇期date是西元0年⾄今多少天(不计算1582年以前)
mysql> select TO_DAYS(950501);
  -> 728779
mysql> select TO_DAYS('1997-10-07');
  -> 729669
FROM_DAYS(N)excel表格制作简历
 给出西元0年⾄今多少天返回DATE值(不计算1582年以前)
mysql> select FROM_DAYS(729669);
  -> '1997-10-07'
DATE_FORMAT(date,format)
 根据format字符串格式化date值
 (在format字符串中可⽤标志符:
 %M ⽉名字(January……December)
 %W 星期名字(Sunday……Saturday)
 %D 有英语前缀的⽉份的⽇期(1st, 2nd, 3rd, 等等。)
 %Y 年, 数字, 4 位
 %y 年, 数字, 2 位
 %a 缩写的星期名字(Sun……Sat)
 %d ⽉份中的天数, 数字(00……31)
 %e ⽉份中的天数, 数字(0……31)
 %m ⽉, 数字(01……12)
 %c ⽉, 数字(1……12)
 %b 缩写的⽉份名字(Jan……Dec)
 %j ⼀年中的天数(001……366)
 %H ⼩时(00……23)
 %k ⼩时(0……23)
 %h ⼩时(01……12)
 %I ⼩时(01……12)
 %l ⼩时(1……12)
 %i 分钟, 数字(00……59)
 %r 时间,12 ⼩时(hh:mm:ss [AP]M)
 %T 时间,24 ⼩时(hh:mm:ss)
 %S 秒(00……59)
 %s 秒(00……59)
 %p AM或PM
 %w ⼀个星期中的天数(0=Sunday ……6=Saturday )
 %U 星期(0……52), 这⾥星期天是星期的第⼀天
 %u 星期(0……52), 这⾥星期⼀是星期的第⼀天
 %% 字符% )
mysql> select DATE_FORMAT('1997-10-04 22:23:00','%W %M %Y');
  -> 'Saturday October 1997'
mysql> select DATE_FORMAT('1997-10-04 22:23:00','%H:%i:%s');
  -> '22:23:00'
mysql> select DATE_FORMAT('1997-10-04 22:23:00','%D %y %a %d %m %b %j');
  -> '4th 97 Sat 04 10 Oct 277'
mysql> select DATE_FORMAT('1997-10-04 22:23:00','%H %k %I %r %T %S %w');
  -> '22 22 10 10:23:00 PM 22:23:00 00 6'
TIME_FORMAT(time,format)
 和DATE_FORMAT()类似,但TIME_FORMAT只处理⼩时、分钟和秒(其余符号产⽣⼀个NULL值或0)
CURDATE()
CURRENT_DATE()
 以'YYYY-MM-DD'或YYYYMMDD格式返回当前⽇期值(根据返回值所处上下⽂是字符串或数字)
mysql> select CURDATE();
  -> '1997-12-15'
mysql> select CURDATE() 0;
  -> 19971215
CURTIME()
CURRENT_TIME()
 以'HH:MM:SS'或HHMMSS格式返回当前时间值(根据返回值所处上下⽂是字符串或数字)
mysql> select CURTIME();
  -> '23:50:26'
mysql> select CURTIME() 0;
  -> 235026
NOW()
SYSDATE()
CURRENT_TIMESTAMP()
 以'YYYY-MM-DD HH:MM:SS'或YYYYMMDDHHMMSS格式返回当前⽇期时间(根据返回值所处上下⽂是字符串或数字)
mysql> select NOW();
  -> '1997-12-15 23:50:26'
mysql> select NOW() 0;
  -> 19971215235026
UNIX_TIMESTAMP()
UNIX_TIMESTAMP(date)
 返回⼀个Unix时间戳(从'1970-01-01 00:00:00'GMT开始的秒数,date默认值为当前时间)
mysql> select UNIX_TIMESTAMP();
  -> 882226357
mysql> select UNIX_TIMESTAMP('1997-10-04 22:23:00');
  -> 875996580
FROM_UNIXTIME(unix_timestamp)
 以'YYYY-MM-DD HH:MM:SS'或YYYYMMDDHHMMSS格式返回时间戳的值(根据返回值所处上下⽂是字符串或数字)
mysql> select FROM_UNIXTIME(875996580);
  -> '1997-10-04 22:23:00'
mysql> select FROM_UNIXTIME(875996580) 0;
  -> 19971004222300
FROM_UNIXTIME(unix_timestamp,format)
 以format字符串格式返回时间戳的值
mysql> select FROM_UNIXTIME(UNIX_TIMESTAMP(),'%Y %D %M %h:%i:%s %x');
  -> '1997 23rd December 03:43:30 x'
SEC_TO_TIME(seconds)
 以'HH:MM:SS'或HHMMSS格式返回秒数转成的TIME值(根据返回值所处上下⽂是字符串或数字)
mysql> select SEC_TO_TIME(2378);
  -> '00:39:38'
mysql> select SEC_TO_TIME(2378) 0;
  -> 3938
TIME_TO_SEC(time)
 返回time值有多少秒
mysql> select TIME_TO_SEC('22:23:00');
  -> 80580
mysql> select TIME_TO_SEC('00:39:38');
  -> 2378
统计函数记录——按⽉、按季度、按⽇、时间段统计
按年汇总,统计:
select sum(mymoney) as totalmoney, count(*) as sheets from mytable group by date_format(col, '%Y');
按⽉汇总,统计:
select sum(mymoney) as totalmoney, count(*) as sheets from mytable group by date_format(col, '%Y-%m');
按季度汇总,统计:
select sum(mymoney) as totalmoney,count(*) as sheets from mytable group by concat(date_format(col, '%Y'),FLOOR((date_format(col, '%m')+2)/3)); select sum(mymoney) as totalmoney,count(*) as sheets from mytable group by concat(date_format(col, '%Y'),FLOOR((date_format(col, '%m')+2)/3));按⼩时:
select sum(mymoney) as totalmoney,count(*) as sheets from mytable group by date_format(col, '%Y-%m-%d %H ');
查询本年度的数据:
SELECT * FROM mytable WHERE year(FROM_UNIXTIME(my_time)) = year(curdate())
查询数据附带季度数:
SELECT id, quarter(FROM_UNIXTIME(my_time)) FROM mytable;
查询本季度的数据:
SELECT * FROM mytable WHERE quarter(FROM_UNIXTIME(my_time)) = quarter(curdate());
本⽉统计:
select * from mytable where month(my_time1) = month(curdate()) and year(my_time2) = year(curdate())
本周统计:
select * from mytable where month(my_time1) = month(curdate()) and week(my_time2) = week(curdate())
N天内记录:
WHERE TO_DAYS(NOW())-TO_DAYS(时间字段)<=N
例⼦:
[sql]
1. -- 全站时段统计
2.
3. SELECT tch.hourlist as hourStr,
risky
4.  IFNULL(tv.numStr,0) as viewCount,
5.  IFNULL(ts.numStr,0) as shareCount,
6.  IFNULL(tl.numStr,0) as likesCount,
7.  IFNULL(tc.numStr,0) as collectCount
8. FROM
9. (SELECT hourlist from t_calendar_hour) as tch
10. LEFT JOIN (select  DATE_FORMAT(CREATE_TIME,'%H') as hourStr, count(ID) as numStr from t_article_view_record where 1=1 and CREATE_TIME>='2016-
06-20 00:00:00'and CREATE_TIME<='2016-06-20 23:59:59'GROUP BY DATE_FORMAT(CREATE_TIME,'%Y-%m-%d %H')) as tv
11. ON tv.hourStr = tch.hourlist
12. LEFT JOIN (select  DATE_FORMAT(CREATE_TIME,'%H') as hourStr, count(ID) as numStr from t_article_share_record where 1=1 and CREATE_TIME>='2016-
06-20 00:00:00'and CREATE_TIME<='2016-06-20 23:59:59'GROUP BY DATE_FORMAT(CREATE_TIME,'%Y-%m-%d %H')) as ts
13. ON ts.hourStr = tch.hourlist
14. LEFT JOIN (select  DATE_FORMAT(CREATE_TIME,'%H') as hourStr, count(ID) as numStr from t_article_likes_record where 1=1 and CREATE_TIME>='2016-
06-20 00:00:00'and CREATE_TIME<='2016-06-20 23:59:59'GROUP BY DATE_FORMAT(CREATE_TIME,'%Y-%m-%d %H')) as tl
15. ON tl.hourStr = tch.hourlist
16. LEFT JOIN (select  DATE_FORMAT(CREATE_TIME,'%H') as hourStr, count(ID) as numStr from t_article_collection_record where 1=1 and CREATE_TIME>='2016-
06-20 00:00:00'and CREATE_TIME<='2016-06-20 23:59:59'GROUP BY DATE_FORMAT(CREATE_TIME,'%Y-%m-%d %H')) as tc
17. ON tc.hourStr = tch.hourlist
18. WHERE 1=1 ORDER BY tch.hourlist ASC;
19.
20.
21.
22. 创建存储过程
23. DELIMITER;//
24. create procedure myproc()
25. begin
26. declare num int;
27. set num=1;
28. while num <= 24 do
29. insert into t_calendar_hour(hourlist) values(num);
30. set num=num+1;
31. end while;
32. commit;
33. end;//
34.
35. 调⽤存储过程
36. CALL myproc();
37.
38. -- ⽂章按时段统计
39.
40. SELECT tch.hourlist as hourStr,
41.  IFNULL(tv.numStr,0) as viewCount,
42.  IFNULL(ts.numStr,0) as shareCount,
43.  IFNULL(tl.numStr,0) as likesCount,
44.  IFNULL(tc.numStr,0) as collectCount
45. FROM
46. (SELECT hourlist from t_calendar_hour) as tch
47. LEFT JOIN (select  DATE_FORMAT(CREATE_TIME,'%H') as hourStr, count(ID) as numStr from t_article_view_record where 1=1 and ARTICLE_ID="e548fdc0556bb01a01556bc6723b0005"and CREATE_TIME>='2016-
06-20 00:00:00'and CREATE_TIME<='2016-06-20 23:59:59'GROUP BY DATE_FORMAT(CREATE_TIME,'%Y-%m-%d %H')) as tv
48. ON tv.hourStr = tch.hourlist
49. LEFT JOIN (select  DATE_FORMAT(CREATE_TIME,'%H') as hourStr, count(ID) as numStr from t_article_share_record where 1=1 and ARTICLE_ID="e548fdc0556bb01a01556bc6723b0005"and CREATE_TIME>='2016-
06-20 00:00:00'and CREATE_TIME<='2016-06-20 23:59:59'GROUP BY DATE_FORMAT(CREATE_TIME,'%Y-%m-%d %H')) as ts
50. ON ts.hourStr = tch.hourlist
51. LEFT JOIN (select  DATE_FORMAT(CREATE_TIME,'%H') as hourStr, count(ID) as numStr from t_article_likes_record where 1=1 and ARTICLE_ID="e548fdc0556bb01a01556bc6723b0005"and CREATE_TIME>='2016-
06-20 00:00:00'and CREATE_TIME<='2016-06-20 23:59:59'GROUP BY DATE_FORMAT(CREATE_TIME,'%Y-%m-%d %H')) as tl
52. ON tl.hourStr = tch.hourlist
53. LEFT JOIN (select  DATE_FORMAT(CREATE_TIME,'%H') as hourStr, count(ID) as numStr from t_article_collection_record where 1=1 and ARTICLE_ID="e548fdc0556bb01a01556bc6723b0005"and CREATE_TIME>='2016-
06-20 00:00:00'and CREATE_TIME<='2016-06-20 23:59:59'GROUP BY DATE_FORMAT(CREATE_TIME,'%Y-%m-%d %H')) as tc
54. ON tc.hourStr = tch.hourlist
55. WHERE 1=1 ORDER BY tch.hourlist ASC;
56.
57.
58. SELECT hourlist from t_calendar_hour;
59.
60. select DATE_FORMAT(CREATE_TIME,'%Y-%m-
%d') as dateStr, DATE_FORMAT(CREATE_TIME,'%H') as hourStr, count(ID) as numStr from t_article_view_record where ARTICLE_ID="e548fdc0556bb01a01556bc6723b0005"and CREATE_TIME>='2016-
06-20 00:00:00'and CREATE_TIME<='2016-06-20 23:59:59'GROUP BY DATE_FORMAT(CREATE_TIME,'%Y-%m-%d %H');
61.
62. select DATE_FORMAT(CREATE_TIME,'%Y-%m-
%d') as dateStr, DATE_FORMAT(CREATE_TIME,'%H') as hourStr, count(ID) as numStr from t_articl
e_share_record where ARTICLE_ID="e548fdc0556bb01a01556bc6723b0005"and CREATE_TIME>='2016-
06-20 00:00:00'and CREATE_TIME<='2016-06-20 23:59:59'GROUP BY DATE_FORMAT(CREATE_TIME,'%Y-%m-%d %H');
63.
64. select DATE_FORMAT(CREATE_TIME,'%Y-%m-
%d') as dateStr, DATE_FORMAT(CREATE_TIME,'%H') as hourStr, count(ID) as numStr from t_article_likes_record where ARTICLE_ID="e548fdc0556bb01a01556bc6723b0005"and CREATE_TIME>='2016-
06-20 00:00:00'and CREATE_TIME<='2016-06-20 23:59:59'GROUP BY DATE_FORMAT(CREATE_TIME,'%Y-%m-%d %H');
65.
66. select DATE_FORMAT(CREATE_TIME,'%Y-%m-
%d') as dateStr, DATE_FORMAT(CREATE_TIME,'%H') as hourStr, count(ID) as numStr from t_article_collection_record where ARTICLE_ID="e548fdc0556bb01a01556bc6723b0005"and CREATE_TIME>='2016-06-20 00:00:00'and CREATE_TIME<='2016-06-20 23:59:59'GROUP BY DATE_FORMAT(CREATE_TIME,'%Y-%m-%d %H');
67.
68.
69.
70.
71. ⽂章数据全站统计
72.    SPECIAL_ID="4028813d54851a470154855335040012"-- 频道
73.    CREATOR="e548fdc054f170770154f17f55dc0008"-- 编辑
74.    AUTHOR="4028813d54084b6a015408a303f1003a"-- 作者
75. and TITLE LIKE"%陷贿选门%"
76.
77.
78. SELECT t.ID as articleId, t.title as title,t.SPECIAL_ID as specialId,t.CREATOR as createId,t.author as authorId,t.FACT_TIME as factTime,
79.  IFNULL(tv.numStr,0) as viewCount,
80.  IFNULL(ts.numStr,0) as shareCount,
81.  IFNULL(tl.numStr,0) as likesCount,
82.  IFNULL(tc.numStr,0) as collectCount
83. FROM
84.    (select ID,TITLE,SPECIAL_ID,CREATOR,AUTHOR,FACT_TIME from t_article WHERE  1=1 and SPECIAL_ID='e548fdc054f188670154f1b6366c000b'ORDER BY FACT_TIME DESC LIMIT 0,10
00) as t
85. LEFT JOIN (SELECT ARTICLE_ID,CREATE_TIME, count(ID) as numStr from t_article_view_record where CREATE_TIME>='2016-9-
16'and CREATE_TIME<='2016-10-16'GROUP BY ARTICLE_ID having numStr>=45 and numStr<=87) as tv
86. on t.ID = tv.ARTICLE_ID
87. LEFT JOIN (SELECT ARTICLE_ID,CREATE_TIME, count(ID) as numStr from t_article_share_record where CREATE_TIME>='2016-9-
16'and CREATE_TIME<='2016-10-16'GROUP BY ARTICLE_ID) as ts
88. on t.ID = ts.ARTICLE_ID
89. LEFT JOIN (SELECT ARTICLE_ID,CREATE_TIME, count(ID) as numStr from t_article_likes_record where CREATE_TIME>='2016-9-
16'and CREATE_TIME<='2016-10-16'GROUP BY ARTICLE_ID) as tl
90. on t.ID = tl.ARTICLE_ID
91. LEFT JOIN (SELECT ARTICLE_ID,CREATE_TIME, count(ID) as numStr from t_article_collection_record where CREATE_TIME>='2016-9-
16'and CREATE_TIME<='2016-10-16'GROUP BY ARTICLE_ID) as tc
92. on t.ID = tc.ARTICLE_ID
93. where 1=1 ;
94.
95.
96. SELECT t.ID as articleId, t.title as title,t.SPECIAL_ID as specialId,t.CREATOR as createId,t.author as authorId,t.FACT_TIME as factTime,IFNULL(tv.numStr,0) as viewCount,IFNULL(ts.numStr,0) as shareCount,IFNULL(tl.numStr,0) as likesCount,IFNUL
06-28'and CREATE_TIME<='2016-10-
12'GROUP BY ARTICLE_ID) as tv on t.ID = tv.ARTICLE_ID  LEFT JOIN (SELECT ARTICLE_ID,CREATE_TIME, count(ID) as numStr from t_article_share_record where 1=1  and CREATE_TIME>='2016-
06-28'and CREATE_TIME<='2016-10-
12'GROUP BY ARTICLE_ID) as ts on t.ID = ts.ARTICLE_ID  LEFT JOIN (SELECT ARTICLE_ID,CREATE_TIME, count(ID) as numStr from t_article_likes_record where 1=1  and CREATE_TIME>='2016-
06-28'and CREATE_TIME<='2016-10-
12'GROUP BY ARTICLE_ID) as tl on t.ID = tl.ARTICLE_ID  LEFT JOIN (SELECT ARTICLE_ID,CREATE_TIME, count(ID) as numStr from t_article_collection_record where 1=1  and CREATE_TIME>='2016-
06-28'and CREATE_TIME<='2016-10-
12'GROUP BY ARTICLE_ID) as tc on t.ID = tc.ARTICLE_ID where 1=1  ORDER BY viewCount DESC  LIMIT 0,20;
97.
98.
99.
100.
101.
102. SELECT ARTICLE_ID,CREATE_TIME, count(ID) as numStr from t_article_view_record where CREATE_TIME>='2016-9-
16'and CREATE_TIME<='2016-10-16'GROUP BY ARTICLE_ID having numStr>=45 and numStr<=87;
103.
104. SELECT ARTICLE_ID,CREATE_TIME, count(ID) as numStr from t_article_share_record where CREATE_TIME>='2016-9-
16'and CREATE_TIME<='2016-10-16'GROUP BY ARTICLE_ID having numStr>=45;
105.
106. SELECT ARTICLE_ID,CREATE_TIME, count(ID) as numStr from t_article_likes_record where CREATE_TIME>='2016-9-
16'and CREATE_TIME<='2016-10-16'GROUP BY ARTICLE_ID having numStr>=45;
107.
108. SELECT ARTICLE_ID,CREATE_TIME, count(ID) as numStr from t_article_collection_record where CREATE_TIME>='2016-9-
16'and CREATE_TIME<='2016-10-16'GROUP BY ARTICLE_ID having numStr>=45;
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124. ⽂章图表数据详情
125. SELECT de.datelist as timeStr,
126. IFNULL(tv.numStr,0) as viewCount,
127. IFNULL(ts.numStr,0) as shareCount,
128. IFNULL(tl.numStr,0) as likesCount,
129. IFNULL(tc.numStr,0) as collectCount FROM
130. (SELECT * from t_calendar where datelist<'2016-07-01'ORDER BY datelist DESC LIMIT 0,100) as de
131. LEFT JOIN (select DATE_FORMAT(CREATE_TIME,'%Y-%m-
%d') as dateStr,count(ID) as numStr from t_article_view_record where ARTICLE_ID="e548fdc0556bb01a01556bc6723b0005"GROUP BY DATE_FORMAT(CREATE_TIME,
'%Y-
%m-%d')) AS tv
132. on tv.dateStr = de.datelist
133. LEFT JOIN (select DATE_FORMAT(CREATE_TIME,'%Y-%m-
%d') as dateStr,count(ID) as numStr from t_article_share_record where ARTICLE_ID="e548fdc0556bb01a01556bc6723b0005"GROUP BY DATE_FORMAT(CREATE_TIME,'%Y-
%m-%d')) AS ts
134. on ts.dateStr = de.datelist
135. LEFT JOIN (select DATE_FORMAT(CREATE_TIME,'%Y-%m-
%d') as dateStr,count(ID) as numStr from t_article_likes_record where ARTICLE_ID="e548fdc0556bb01a01556bc6723b0005"GROUP BY DATE_FORMAT(CREATE_TIME,'%Y-
mysql面试题大全
%m-%d')) AS tl
136. on tl.dateStr = de.datelist
137. LEFT JOIN (select DATE_FORMAT(CREATE_TIME,'%Y-%m-
%d') as dateStr,count(ID) as numStr from t_article_collection_record where ARTICLE_ID="e548fdc0556bb01a01556bc6723b0005"GROUP BY DATE_FORMAT(CREATE_TIME,'%Y-
%m-%d')) AS tc
138. on tc.dateStr = de.datelist
139. ORDER BY de.datelist DESC;
140.
141.
142. -- 按⽇统计
143. select DATE_FORMAT(CREATE_TIME,'%Y-%m-
%d') as dateStr,count(ID) as numStr from t_article_view_record where ARTICLE_ID="e548fdc0556bb01a01556bc6723b0005"GROUP BY DATE_FORMAT(CREATE_TIME,'%Y-
%m-%d');
144.
145. select DATE_FORMAT(CREATE_TIME,'%Y-%m-
%d') as dateStr,count(ID) as numStr from t_article_share_record where ARTICLE_ID="e548fdc0556bb01a01556bc6723b0005"GROUP BY DATE_FORMAT(CREATE_TIME,'%Y-
%m-%d');
146.
147. select DATE_FORMAT(CREATE_TIME,'%Y-%m-
%d') as dateStr,count(ID) as numStr from t_article_likes_record where ARTICLE_ID="e548fdc0556bb01a01556bc6723b0005"GROUP BY DATE_FORMAT(CREATE_TIME,'%Y-
%m-%d');
148.
149. select DATE_FORMAT(CREATE_TIME,'%Y-%m-
%d') as dateStr,count(ID) as numStr from t_article_collection_record where ARTICLE_ID="e548fdc0556bb01a01556bc6723b0005"GROUP BY DATE_FORMAT(CREATE_TIME,'%Y-
%m-%d');
150.
151. select * from t_article_share_record where ARTICLE_ID="e548fdc0556bb01a01556bc6723b0005";
原⽂地址:blog.csdn/qq_22562651/article/details/80621286

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。