First, if someone has a better title please help.
If I have let’s say a ‘calendar’ table with a ‘day’ column. And I have the following query:
SELECT day, day AS testDay, testDay AS test2Day FROM calendar
MySQL will complain that “testDay” is an unknown column. Of course you will tell me that this statement is useless, but my statement looks more like this:
SELECT day, SOME_CRAZY_EXPRESSION_OF(day) AS testDay, EXPRESSION_OF(testDay) AS test2Day FROM calendar
And the point is I don’t want to evaluate twice the first expression to use it within the second expression.. So is there a way to use a value calculated in the select as part of the select itself?
Of course I could do:
SELECT day, SOME_CRAZY_EXPRESSION_OF(day) AS testDay, EXPRESSION_OF(SOME_CRAZY_EXPRESSION_OF(day)) AS test2Day FROM calendar
But I’m trying to avoid wasting. If I have no choice, that’s what I’ll do.
In MySQL, you’ll want to assign a temporary variable to the value of the expression to be able to reuse it, this should do it;
See this page for another example with more analysis on the performance impact.