DAYOFWEEK(date)
返回日期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
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");
-> 1999
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)
给出西元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
返回日期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
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");
-> 1999
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)
给出西元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
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
例子:
- -- 全站时段统计
- SELECT tch.hourlist as hourStr,
- IFNULL(tv.numStr,0) as viewCount,
- IFNULL(ts.numStr,0) as shareCount,
- IFNULL(tl.numStr,0) as likesCount,
- IFNULL(tc.numStr,0) as collectCount
- FROM
- (SELECT hourlist from t_calendar_hour) as tch
- 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
- ON tv.hourStr = tch.hourlist
- 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
- ON ts.hourStr = tch.hourlist
- 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
- ON tl.hourStr = tch.hourlist
- 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
- ON tc.hourStr = tch.hourlist
- WHERE 1=1 ORDER BY tch.hourlist ASC;
- 创建存储过程
- DELIMITER;//
- create procedure myproc()
- begin
- declare num int;
- set num=1;
- while num <= 24 do
- insert into t_calendar_hour(hourlist) values(num);
- set num=num+1;
- end while;
- commit;
- end;//
- 调用存储过程
- CALL myproc();
- -- 文章按时段统计
- SELECT tch.hourlist as hourStr,
- IFNULL(tv.numStr,0) as viewCount,
- IFNULL(ts.numStr,0) as shareCount,
- IFNULL(tl.numStr,0) as likesCount,
- IFNULL(tc.numStr,0) as collectCount
- FROM
- (SELECT hourlist from t_calendar_hour) as tch
- 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
- ON tv.hourStr = tch.hourlist
- 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
- ON ts.hourStr = tch.hourlist
- 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
- ON tl.hourStr = tch.hourlist
- 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
- ON tc.hourStr = tch.hourlist
- WHERE 1=1 ORDER BY tch.hourlist ASC;
- SELECT hourlist from t_calendar_hour;
- 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');
- select DATE_FORMAT(CREATE_TIME,'%Y-%m-%d') as dateStr, DATE_FORMAT(CREATE_TIME,'%H') as hourStr, count(ID) as numStr from t_article_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');
- 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');
- 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');
- 文章数据全站统计
- SPECIAL_ID="4028813d54851a470154855335040012" -- 频道
- CREATOR="e548fdc054f170770154f17f55dc0008" -- 编辑
- AUTHOR="4028813d54084b6a015408a303f1003a" -- 作者
- and TITLE LIKE "%陷贿选门%"
- 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,
- IFNULL(tc.numStr,0) as collectCount
- FROM
- (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,1000) as t
- 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
- on t.ID = tv.ARTICLE_ID
- 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
- on t.ID = ts.ARTICLE_ID
- 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
- on t.ID = tl.ARTICLE_ID
- 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
- on t.ID = tc.ARTICLE_ID
- where 1=1 ;
- 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,IFNULL(tc.numStr,0) as collectCount FROM (select ID,TITLE,SPECIAL_ID,CREATOR,AUTHOR,FACT_TIME from t_article WHERE 1=1 ORDER BY FACT_TIME DESC ) as t LEFT JOIN (SELECT ARTICLE_ID,CREATE_TIME, count(ID) as numStr from t_article_view_record where 1=1 and CREATE_TIME>='2016-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;
- 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;
- 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;
- 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;
- 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;
- 文章图表数据详情
- SELECT de.datelist as timeStr,
- IFNULL(tv.numStr,0) as viewCount,
- IFNULL(ts.numStr,0) as shareCount,
- IFNULL(tl.numStr,0) as likesCount,
- IFNULL(tc.numStr,0) as collectCount FROM
- (SELECT * from t_calendar where datelist<'2016-07-01' ORDER BY datelist DESC LIMIT 0,100) as de
- 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
- on tv.dateStr = de.datelist
- 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
- on ts.dateStr = de.datelist
- 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-%m-%d')) AS tl
- on tl.dateStr = de.datelist
- 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
- on tc.dateStr = de.datelist
- ORDER BY de.datelist DESC;
- -- 按日统计
- 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');
- 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');
- 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');
- 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');
- select * from t_article_share_record where ARTICLE_ID="e548fdc0556bb01a01556bc6723b0005";