Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding MySQL alias fields together

Tags:

Consider a query similar to:

 SELECT sum(EXPR) as total,         sum(EXPR) as total2,          sum(total+total2) as grandtotal   FROM tablename 

This comes up and says unknown column total in field list.

Is there anyway to reference the alias fields in a calculation without retyping the sum expression because sum(EXPR) on each side is very long.

like image 690
Wright-Geek Avatar asked Jul 30 '10 16:07

Wright-Geek


People also ask

Can you GROUP BY alias in SQL?

If mixed-case letters or special symbols, or spaces are required, quotes must be used. Column aliases can be used for derived columns. Column aliases can be used with GROUP BY and ORDER BY clauses. We cannot use a column alias with WHERE and HAVING clauses.

How do I give an alias to multiple columns in SQL?

You can alias an expression or column, such as in select u.id as user_id and select upper(u. username) as uppercase_name , and you can alias a table as in from users u . There exists no group alias for multiple columns.

What is the benefit of using an alias in MySQL?

Advantages of MySQL AliasesIt makes the column or table name more readable. It is useful when you use the function in the query. It can also allow us to combines two or more columns. It is also useful when the column names are big or not readable.


2 Answers

Here's the order of how things are executed in a database engine.

Note that this is a semantic view of how things are executed, the database might do things in a different order, but it has to produce results as though it was done this way.

  1. First the FROM-part is evaluated, where do I get data from
  2. Then the WHERE-part is evaluated, which rows are we interested in
  3. Then the GROUP BY-part is evaluated, how do we combine the resulting rows
  4. Then the HAVING-part is evaluated, which groups are we interested in
  5. Then the ORDER BY-part is evaluated, which order do we want those rows/groups
  6. Finally, the SELECT-part is evaluated, which columns are we interested in

Some database engines allows you to circumvent this though, by saing "GROUP BY 2" to group by the 2nd column in the SELECT-part, but if you stick to the above order, you should know by now that the reason that your code doesn't work is that there are no columns with the names total or total2 (yet).

In other words, you need to either repeat the two expressions, or find another way of doing it.

What you can do is to use a sub-query (providing you're on a MySQL version that supports this):

SELECT total, total2, total+total2 as grandtotal FROM (     SELECT sum(EXPR) as total, sum(EXPR) as total2     FROM tablename     ) x 

Striking out the rest as per the comment.

I don't know much about MySQL though so you might have to alias the sub-query:

...      FROM tablename     ) AS x       ^-+^         |         +-- add this 

Some database engines also disallow using the keyword AS when aliasing subqueries, so if the above doesn't work, try this:

...      FROM tablename     ) x       ^       |       +-- add this 

like image 151
Lasse V. Karlsen Avatar answered Sep 24 '22 22:09

Lasse V. Karlsen


SELECT total, total2, total + total2 as grandtotal from (  SELECT sum(EXPR) as total,          sum(EXPR) as total2,    FROM tablename  ) x 
like image 22
Fosco Avatar answered Sep 20 '22 22:09

Fosco