Using an Alias in SQL Calculations

52,605

Solution 1

Simply wrap your reused alias with (SELECT alias) :

SELECT 10 AS my_num, 
       (SELECT my_num) * 5 AS another_number
FROM table

Solution 2

You'll need to use a subselect to use that aliases that way

SELECT my_num*5 AS another_number FROM
(
    SELECT 10 AS my_num FROM table
) x

Solution 3

Aliases in sql are not like variables in a programming language. Aliases can only be referenced again at certain points (particularly in GROUP BY and HAVING clauses). But you can't reuse an alias in the SELECT clause. So you can use a derived query (such as suggested by Rubens Farias) which lets you basically rename your columns and/or name any computed columns.

Or you could use a VIEW if your formulas are generally fixed

CREATE VIEW table10 AS SELECT 10 AS my_num FROM table;
SELECT my_num * 5 AS another_number FROM table10;

I believe that will be slightly faster than using a derived query but it probably depends a lot on your real query.

Or you could just duplicate the work:

SELECT 10 AS my_num, 10 * 5 AS another_number FROM table;

Which 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";
Share:
52,605
Tom Rossi
Author by

Tom Rossi

Updated on July 11, 2022

Comments

  • Tom Rossi
    Tom Rossi almost 2 years

    Why won't this query work?

    SELECT 10 AS my_num, my_num*5 AS another_number
    FROM table
    

    In this example, I'm trying to use the my_num alias in other calculations. This results in unknown column "my_num"

    This is a simplified version of what I am trying to do, but basically I would like to use an alias to make other calculations. My calculations are much more complicated and thats why it would be nice to alias it since I repeat it several times different ways.

  • Rubens Farias
    Rubens Farias almost 11 years
    @RyanHeneise, it's a subselect alias; that way you could go with x.my_num
  • Carlos Gant
    Carlos Gant almost 10 years
    I am wondering where that functionality is documented. I've searched in the whole MySQL documentation and I have not found it. I don't know how this "alias reusing" works and I am curious about portability or performance impact. If someone knows more about this I will appreciate a link. Thanks you.
  • Eaten by a Grue
    Eaten by a Grue about 8 years
    this just made my week.. maybe month. super useful especially with aliases built from flow control statements like IF(), CASE(), etc
  • DCoder
    DCoder almost 8 years
    @CarlosGant: The (SELECT alias) part is treated as a "dependent subquery", at least according to MySQL's EXPLAIN and Visual Explain functions.
  • Pang
    Pang over 7 years
    Note that CROSS APPLY does not appear to be available in MySQL (yet).
  • Captain Hypertext
    Captain Hypertext about 7 years
    Nevermind, it didn't work for me. I got "unknown column".
  • Ray Coder
    Ray Coder about 3 years
    SELECT kabupaten.nama AS kabupaten, kecamatan.nama AS kecamatan, SUM(pihaks.luas) AS target_luas, SUM(realisasi.luas) AS realisasi_luas, SELECT(realisasi_luas)/SELECT(target_luas)*100 AS persentase_luas. Tried like this but not working
  • Aaron Franke
    Aaron Franke almost 3 years
    This doesn't work for me if I try to use SELECT multiple times. SELECT Users.Id, SUM(Posts.Score) AS ScoreSum, SUM(Posts.ViewCount) AS ViewSum, (SELECT(ViewSum) / SELECT(ScoreSum)) AS ViewScoreRatio says "Incorrect syntax near SELECT" for the second SELECT.