MySQL:查询每周获取日志条目数——包括空集
我的网络应用程序有一个报告,显示特定用户每周的登录次数。然而,我正在努力让查询正确。我遇到的问题是,我似乎无法获取用户未登录的周数。
目前我的查询如下所示:
SELECT
DATE_ADD(logDate, INTERVAL(1-DAYOFWEEK(logDate)) DAY) weekStart,
DATE_ADD(logDate, INTERVAL(7-DAYOFWEEK(logDate)) DAY) weekEnd,
COUNT(*) loginCount
FROM log
WHERE
logDate > $startDate AND
logDate < $endDate
My web application has a report that shows the number of logins by a particular user each week. However, I'm struggling to get the query just right. The problem I'm running into is that I can't seem to get the weeks during which the user did not login at all.
Currently my query looks like this:
SELECT
DATE_ADD(logDate, INTERVAL(1-DAYOFWEEK(logDate)) DAY) weekStart,
DATE_ADD(logDate, INTERVAL(7-DAYOFWEEK(logDate)) DAY) weekEnd,
COUNT(*) loginCount
FROM log
WHERE
logDate > $startDate AND
logDate < $endDate
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将创建一个周数表:
然后用整数值 0..53 填充该表。
然后使用
WEEK()
函数:如果需要此查询支持跨越多年的日期范围,请使用
YEARWEEK()
相反,并用更多行填充临时表YYYYWW 格式类似于YEARWEEK()
返回的值。I would create a table for week numbers:
Then populate that table with integer values 0..53.
Then join it to your log table using the
WEEK()
function:If you need this query to support a date range that spans multiple years, use
YEARWEEK()
instead, and populate your temp table with more rows in the YYYYWW format like values returned byYEARWEEK()
.