SQLite Forum

Getting rid of double DATE()
Login
This goes wrong when there is no record for today. Rewrote it to:
    WITH now(today)
    AS (
        SELECT DATE('now', 'localtime')
    ),
    cte(today, idx)
    AS (
        SELECT (SELECT today FROM now)
        ,      IFNULL(MAX(lastUsedIdx), 0) + 1
        FROM   selectRandom, now
        WHERE  lastUsed == today
    )
    UPDATE selectRandom
    SET    (lastUsed, lastUsedIdx) = (SELECT today, idx FROM cte)
    WHERE  description = :teaToBrew

Or would it be better to use:
        FROM   selectRandom
        WHERE  lastUsed == (SELECT today FROM now)