PHP 5.X,MySQL 5.7.15
根据星期几对数据进行分组,以使用最大日期和最短日期提供每日使用的平均会话数。在这种情况下,最大和最小日期将应用于星期几,这会导致每个案例的周数不同。
构建这些结果的查询在下面定义为查询1 。基本上,最后4列中的信息应该是静态的,我使用 SELF JOIN 完成了这一点,详情如下。在该示例中, session_count 爆炸,因为我只在开发环境中工作。
出了什么问题,为什么?
session_count | weekday | maxDate |minDate | numOfDayDiff| numOfWeeks
'29' | 'Friday'|'2017-03-10 12:16:47'|'2016-08-12 12:31:28'| '210' | '30'
'26' |'Monday' |'2017-03-06 17:10:59'|'2016-08-08 14:31:16'| '210' | '30'
'6' |'Saturday'|'2017-03-04 23:26:12'|'2016-08-20 23:10:47'| '196' | '28'
'10' |'Sunday' |'2017-03-12 18:28:51'|'2016-08-14 16:26:30'| '210' | '30'
'22' |'Thursday'|'2017-03-09 15:45:27'|'2016-06-30 12:42:19'| '252' | '36'
'22' |'Tuesday'|'2017-03-21 15:00:08'|'2016-06-28 19:51:21'| '266' | '38'
'22' |'Wednesday'|'2017-03-01 23:57:34'|'2016-07-06 16:17:39'| '238' | '34'
我希望最大日期和分钟日期是静态的。我曾尝试使用w3schools解释Self JOIN Syntax
SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;
我尝试应用此查询,maxDate,minDate,numDaysDiff和numOfWeeks与上面最高周数的值一致,但 session_count 的统计数据爆炸。
以下是获得上表的查询1 :
SELECT
CEIL(COUNT(ss.session_id) / (FLOOR (DATEDIFF(MAX(ss.date),MIN(ss.date))/7))) as session_count,
DAYNAME((ss.date)) as weekday,
MAX(ss.date) as maxDate,
MIN(ss.date) as minDate,
DATEDIFF(MAX(ss.date),MIN(ss.date)) as numOfDayDiff,
FLOOR(DATEDIFF(MAX(ss.date),MIN(ss.date))/7) as numOfWeeks
FROM session ss
JOIN user u ON ss.user = u.id
JOIN account a on u.account = a.id
WHERE a.isTest=false
GROUP BY weekday
ORDER BY weekday;
使用 SELF JOIN 语法修改了 Q-2 ,后跟输出表:
SELECT
CEIL(COUNT(ss.session_id) / (FLOOR (DATEDIFF(MAX(xx.date),MIN(xx.date))/7))) as session_count,
DAYNAME((ss.date)) as weekday,
MAX(xx.date) as maxDate,
MIN(xx.date) as minDate,
DATEDIFF(MAX(xx.date),MIN(xx.date)) as numOfDayDiff,
FLOOR(DATEDIFF(MAX(xx.date),MIN(xx.date))/7) as numOfWeeks
FROM session xx, session ss
JOIN user u ON ss.user = u.id
JOIN account a on u.account = a.id
WHERE a.isTest=false
GROUP BY weekday
ORDER BY weekday;
结果是:
session_count | weekday | maxDate |minDate | numOfDayDiff| numOfWeeks
99357 |Friday |2017-03-21 15:00:08 |2016-06-28 19:51:21 |266 |38
88062 |Monday |2017-03-21 15:00:08 |2016-06-28 19:51:21 |266 |38
16829 |Saturday |2017-03-21 15:00:08 |2016-06-28 19:51:21 |266 |38
32505 |Sunday |2017-03-21 15:00:08 |2016-06-28 19:51:21 |266 |38
90136 |Thursday |2017-03-21 15:00:08 |2016-06-28 19:51:21 |266 |38
94516 |Tuesday |2017-03-21 15:00:08 |2016-06-28 19:51:21 |266 |38
83451 |Wednesday|2017-03-21 15:00:08 |2016-06-28 19:51:21 |266 |38
任何人都可以告诉我我是如何犯错的吗?我将看看我是否可以创建一个以附加的mysql小提琴为例。
答案 0 :(得分:0)
为了减轻数据库压力,我决定使用查询1 的修改版本,并使用结果来确定哪一周的周数最多,并将计数除以周数在php。
以下是修改后的查询1
char
注意数据因数据库不同而不同。