Problem Description
I've got the following MySQL query:
SELECT concat('<a target="_new" href="%%WWWROOT%%/course/user.php?id=1&user=',
u.id,
'&mode=alllogs">',
u.firstname ,' ',
u.lastname,'</a>') AS Username,
count(*) AS logins ,
(SELECT count(*)
FROM mdl_log
WHERE userid = l.userid
GROUP BY userid) AS Activity
FROM mdl_log AS l
JOIN mdl_user AS u ON l.userid = u.id
WHERE action LIKE '%login%'
GROUP BY userid
ORDER BY Activity DESC
INTO OUTFILE '/tmp/Total_Logins_With_Total_Activity.txt';
I want to extend this so that I can results based on a data range, say 365 days for instance.
So I'm thinking I can alter the above query to select the 'time' field from the mdl_logs table and covert that to days (Its a unix timestamp) and then only select when the time is within the last 365 day period. Any help appreciated.
The **mdl_log** table has the following structure:
+--------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| time | int(10) unsigned | NO | MUL | 0 | |
| userid | int(10) unsigned | NO | MUL | 0 | |
| ip | varchar(15) | NO | | | |
| course | int(10) unsigned | NO | MUL | 0 | |
| module | varchar(20) | NO | | | |
| cmid | int(10) unsigned | NO | MUL | 0 | |
| action | varchar(40) | NO | MUL | | |
| url | varchar(100) | NO | | | |
| info | varchar(255) | NO | | | |
+--------+------------------+------+-----+---------+----------------+
The **mdl_user** table looks like:
+---------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| auth | varchar(20) | NO | MUL | manual | |
| confirmed | tinyint(1) | NO | MUL | 0 | |
| policyagreed | tinyint(1) | NO | | 0 | |
| deleted | tinyint(1) | NO | MUL | 0 | |
| mnethostid | bigint(10) | NO | MUL | 0 |...
AI-Generated Solution
Powered by LMSouq AI · GPT-4.1-mini
Analyzing problem and generating solution…
Was this solution helpful?