Using Mysql this statement works:
SELECT *, ABS(UNIX_TIMESTAMP(time) -
UNIX_TIMESTAMP('2013-11-14 14:35:21') ) as diff from Alert
order by diff
limit 3
But if I add a where clause as follows, it gives "Unknown column 'diff' in 'where clause'" error
SELECT *, ABS(UNIX_TIMESTAMP(time) -
UNIX_TIMESTAMP('2013-11-14 14:35:21') ) as diff from Alert
where diff < 2
order by diff
limit 3
Ok I change the statement to using the Having clause as follows:
SELECT *, ABS(UNIX_TIMESTAMP(time) -
UNIX_TIMESTAMP('2013-11-14 14:35:21') ) as diff from Alert
order by diff
having diff<2
It is still giving me You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'having diff<2' at line 4.
Use
You can't use the named column computation in the order by (or where) clause.