Aliases in sql are not variables in a programming language. Aliases can only be specified at specific points (especially in GROUP BY and HAVING ). But you cannot reuse an alias in a SELECT . That way, you can use a derived query (like the one suggested by Rubens Farias ), which allows you to basically rename your columns and / or specify any calculated columns.
Or you can use VIEW if your formulas are usually fixed.
CREATE VIEW table10 AS SELECT 10 AS my_num FROM table; SELECT my_num * 5 AS another_number FROM table10;
I believe this will be a little faster than using a derived query, but it probably depends a lot on your real request.
Or you can just duplicate the work:
SELECT 10 AS my_num, 10 * 5 AS another_number FROM table;
What might be convenient in something like php / perl:
my $my_num = 10; my $query = "SELECT $my_num AS my_num, $my_num * 5 AS another_number FROM table";
Rob Van Dam Jan 16 '10 at 18:22 2010-01-16 18:22
source share