I have the following query within inside a select if in my code and I can’t remember what the line containing @lastComment := time actually does?
SELECT
IF(
(SELECT @lastComment := `time`
FROM usermessages
WHERE userId = $userId
ORDER BY id DESC
LIMIT 1)
IS NOT NULL,
DATE_SUB(NOW(), INTERVAL 30 SECOND) >= @lastComment, 1
)
It looks like @lastComment is just a temporary variable holding time but I don’t understand why it’s needed, is it just to pass it to the other query?
Obviously you didn’t want to execute the query twice and that’s correct but you can easily omit the user-defined variable and thus make it clearer:
which translates to: check if the given expression is NULL, if so return 1, else return the expression value.