Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename a select column in sql

Tags:

sql

mysql

I have a question about SQL. Here is the case: I have a table with 5 columns (C1...C5) I want to do

 select (C1+C2*3-C3*5/C4) from table;

Is there any way of naming the resulting column for referring later in a query ?

like image 699
Jesh Avatar asked Aug 02 '11 12:08

Jesh


People also ask

How do I rename a column in SQL Server?

Use SQL Server Management Studio In Object Explorer, connect to an instance of Database Engine. In Object Explorer, right-click the table in which you want to rename columns and choose Rename. Type a new column name.

How do you change a column name?

To change a column name, enter the following statement in your MySQL shell: ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name; Replace table_name , old_column_name , and new_column_name with your table and column names.


1 Answers

SELECT (C1+C2*3-C3*5/C4) AS formula FROM table;

You can give it an alias using AS [alias] after the formula. If you can use it later depends on where you want to use it. If you want to use it in the where clause, you have to wrap it in an outer select, because the where clause is evaluated before your alias.

SELECT * 
FROM (SELECT (C1+C2*3-C3*5/C4) AS formula FROM table) AS t1
WHERE formula > 100
like image 174
Jacob Avatar answered Sep 21 '22 21:09

Jacob