我的网络服务器上有一个MOODLE。我想每周和每月跟踪所述资源的使用情况。用法将基于用户的总读取和写入。我将从我的MOODLE数据库的mdl_stats_user_daily
表中获取数据。
我想给那些将进入前10名的人提供一些确认和令牌。我有一个现有的PHP脚本连接到数据库并执行查询以获得我的结果但似乎我的查询是错误的。这是我的疑问:
$query = "SELECT user.firstname, user.lastname, user.userid, stats.userid, stats.roleid,
SUM( statsreads ) AS numreads, SUM( statswrites ) AS numwrites,
SUM( statsreads ) + SUM( statswrites ) AS totalactivity
FROM `mdl_stats_user_daily` stats, `mdl_user` user
WHERE userid IN (SELECT userid
FROM mdl_role_assignments
WHERE roleid IN (1,2,3,4))
AND user.id = stats.userid
AND stats.timeend < ".(time() - 604800)."
GROUP BY userid ORDER BY totalactivity DESC Limit 20";
你可以看到那一行:
Where userid IN (SELECT userid
FROM mdl_role_assignments
WHERE roleid IN (1,2,3,4))
AND user.id = stats.userid
AND stats.timeend < ".(time() - 604800)".
".(time() - 604800)"
是我用来过滤结果的行,只是过去一周的使用情况。 &#34; 604800&#34;是一周或7天内的秒数。但似乎我错了,即使我改变了数字,我的查询仍然会返回相同的结果。有人能帮助我吗?
答案 0 :(得分:0)
尝试使用此查询: -
SELECT user.firstname, user.lastname, user.userid, stats.userid, stats.roleid,
SUM( statsreads ) AS numreads, SUM( statswrites ) AS numwrites,
SUM( statsreads ) + SUM( statswrites ) AS totalactivity
FROM `mdl_stats_user_daily` stats, `mdl_user` user
WHERE userid IN (SELECT userid
FROM mdl_role_assignments
WHERE roleid IN (1,2,3,4))
AND user.id = stats.userid
AND stats.timeend < ADDDATE(CURDATE(), -7)
GROUP BY userid ORDER BY totalactivity DESC Limit 20"
希望这可以帮到你。
答案 1 :(得分:0)
正如EricBouwers所说,它是&gt;而不是&lt;
但是你也应该使用角色名而不是角色id,因为id可能会改变。
EXISTS而不是IN对性能更好。
SELECT user.id,
user.firstname,
user.lastname,
SUM(statsreads) AS numreads,
SUM(statswrites) AS numwrites,
SUM(statsreads) + SUM(statswrites) AS totalactivity
FROM mdl_stats_user_daily stats
JOIN mdl_user user ON user.id = stats.userid
WHERE EXISTS (SELECT ra.userid
FROM mdl_role_assignments ra
JOIN mdl_role r ON r.id = ra.roleid AND r.shortname IN ('student')
WHERE ra.userid = user.id)
AND stats.timeend > NOW() - (7*24*60*60)
GROUP BY user.id, user.firstname, user.lastname
ORDER BY totalactivity DESC
LIMIT 20
答案 2 :(得分:0)
在您和其他人的帮助下,我能够找出正确的查询以获得我想要的输出。非常感谢。这是我对每周报告的最终查询:
$query = "SELECT user.firstname, user.lastname, stats.userid, stats.roleid,
SUM( statsreads ) AS numreads, SUM( statswrites ) AS numwrites,
SUM( statsreads ) + SUM( statswrites ) AS totalactivity
FROM `mdl_stats_user_daily` stats, `mdl_user` user
WHERE userid IN (SELECT userid
FROM mdl_role_assignments
WHERE roleid IN (1,2,3,4))
AND user.id = stats.userid
AND WEEK(FROM_UNIXTIME(stats.timeend)) = (WEEK(CURDATE())-1)
GROUP BY userid ORDER BY totalactivity DESC";
然后是月报:
$query = "SELECT user.firstname, user.lastname, stats.userid, stats.roleid,
SUM( statsreads ) AS numreads, SUM( statswrites ) AS numwrites,
SUM( statsreads ) + SUM( statswrites ) AS totalactivity
FROM `mdl_stats_user_daily` stats, `mdl_user` user
WHERE userid IN (SELECT userid
FROM mdl_role_assignments
WHERE roleid IN (1,2,3,4))
AND user.id = stats.userid
AND MONTH(FROM_UNIXTIME(stats.timeend)) = (MONTH(CURDATE())-1)
GROUP BY userid
ORDER BY totalactivity DESC";
希望这可以帮助未来的用户。