I have columns in my database for date_created (format 2014-07-08 11:25:29) and username. I want to create a conditional statement which will display users who have not submitted a new record within the last week. I've never really queried my database for an action NOT taking place. How might I achieve this?
SELECT username
FROM records
GROUP BY
username
HAVING MAX(date_created) < NOW() - INTERVAL 1 WEEK
Note, however, that this query won't return users who haven't created a single record.
If you have another table holding all user records (including those who had no records in records
), use this query:
SELECT u.username
FROM users u
WHERE NOT EXISTS
(
SELECT NULL
FROM records r
WHERE r.username = u.username
AND r.date_created >= NOW() - INTERVAL 1 WEEK
)
In both cases, make sure you have an index on records (username, date_created)
for the queries to work fast.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.