So, I have a query that looks something like this:
SELECT id,
DATE_FORMAT(CONVERT_TZ(callTime,'+0:00','-7:00'),'%b %d %Y') as callDate,
DATE_FORMAT(CONVERT_TZ(callTime,'+0:00','-7:00'),'%H:%i') as callTimeOfDay,
SEC_TO_TIME(callLength) as callLength
FROM cs_calldata WHERE
customerCode='999999-abc-blahblahblah' AND
CONVERT_TZ(callTime,'+0:00','-7:00') >= '2010-04-25' AND
CONVERT_TZ(callTime,'+0:00','-7:00') <= '2010-05-25'
If you are like me, you are probably starting to think that maybe this will improve the readability and possibly the performance of this query, unless I ask it to be calculated CONVERT_TZ(callTime,'+0:00','-7:00')four separate times.
So, I'm trying to create a column alias for this expression and replace subsequent events with this alias:
SELECT id,
CONVERT_TZ(callTime,'+0:00','-7:00') as callTimeZoned,
DATE_FORMAT(callTimeZoned,'%b %d %Y') as callDate,
DATE_FORMAT(callTimeZoned,'%H:%i') as callTimeOfDay,
SEC_TO_TIME(callLength) as callLength
FROM cs_calldata WHERE
customerCode='5999999-abc-blahblahblah' AND
callTimeZoned >= '2010-04-25' AND
callTimeZoned <= '2010-05-25'
This is when I found out to quote the MySQL manual:
Standard SQL prohibits references to column aliases in the WHERE clause. This restriction is imposed because when the WHERE clause is evaluated, the column value may not have been determined yet.
So this approach seems to be dead in the water.
- , , ?