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.

没有正确的解决方案

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top