Question

I've got a query that returns the time since a last logged 'action'?

SELECT unix_timestamp(now()) - unix_timestamp(dateTime)
FROM action_table
WHERE action='$action'
ORDER BY dateTime DESC
LIMIT 1

The only issue is it returns 0 if no row was found.

Any ideas on how I could get this to return no results if no rows are in the table for the action requested?

EDIT: this is actually working how I wanted it to, returns no results if no action is in the table. My php was just screwing it up when casting the result to an int.

No correct solution

OTHER TIPS

Well, I would use an IF..ELSE Statement like this

SELECT CASE WHEN <br>
   (SELECT 
      unix_timestamp(now()) - unix_timestamp(dateTime)
    FROM action_table
    WHERE action='aaa'
    ORDER BY dateTime DESC
    LIMIT 1) > 0
THEN 
   (SELECT 
      unix_timestamp(now()) - unix_timestamp(dateTime)
      FROM action_table
      WHERE action='aaa'
      ORDER BY dateTime DESC
      LIMIT 1)
ELSE 
   'no result' 
END;

Here is a working example on sqlfiddle.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top