I have a table with a lastaccess date in a unix format. I would like to query all the records in the table where the lastaccess date is greater than 180 days.
This is my current query
SELECT id, auth, username, IF(DATEDIFF(NOW(), FROM_UNIXTIME(lastaccess))>=180,'Y','N') As NotAccessedIn6Months
FROM mdl_user
WHERE auth = 'manual'
I need to be able to use the NotAccessedIn6Months result to filter the resultset, like
SELECT id, auth, username, IF(DATEDIFF(NOW(), FROM_UNIXTIME(lastaccess))>=180,'Y','N') As NotAccessedIn6Months
FROM mdl_user
WHERE auth = 'manual' AND NotAccessedIn6Months = 'Y'
but the latter format is illegal.
Thank you.
You have two options: either use HAVING instead of WHERE, like this:
… or just move this condition into WHERE (as you won’t show ‘N’ values at all in this particular query), like that: