Search code examples
mysql

Avoid using functions for same result in mySql


I wrote this query:

SELECT e.evt_end, UNIX_TIMESTAMP(e.evt_end) AS unixTimeEND FROM events AS e   
WHERE UNIX_TIMESTAMP(e.evt_end) > '.time().' ORDER BY unixtimeEND;   

I use two times the function UNIX_TIMESTAMP() to have the same result, and maybe it's a waste.
Is there a better way? I thought something like:

SELECT e.evt_end, UNIX_TIMESTAMP(e.evt_end) AS unixTimeEND FROM events AS e   
WHERE unixTimeEND > '.time().' ORDER BY unixtimeEND;  

but didn't work


Solution

  • Obviously your second query won't work. It will return column 'unixTimeEND' not found in WHERE clause.

    You can only use column aliases in GROUP BY, ORDER BY, or HAVING clauses.

    Standard SQL doesn't allow you to refer to a column alias in a WHERE clause. This restriction is imposed because when the WHERE code is executed, the column value may not yet be determined.