How can I reuse a computed column in SQL in MySQL? Say: my query is something like: -
SELECT
CONVERT_TZ(
if(timestamp_start > last_update, timestamp_start, last_update),
'GMT',
user.timezone
) as time_usr_tz
from
shecdule
inner join user on shecdule.user_id = user.user_id
where
CONVERT_TZ(
if(timestamp_start > last_update, timestamp_start, last_update),
'GMT',
user.timezone
)
< CURRENT_TIMESTAMP();
If you see the query the "CONVERT_TZ....." part is repeated.
This is only a sample query. Actually, I have to use that computed column several times. So if I make change in one place, I have to change in many places. And the size of the query becomes scary, too.
Is there any way to avoid such duplication?
UPDATE I already have sub-query in my original query, so sub-query is not a preferable option. Is it possible in any other way?