Your syntax would create an alias for a as b, but it wouldn't have scope beyond the results of the statement. It sounds like you may want to create a VIEW
SELECT only retrieves data from the database, it does not change the table itself.
If you write
SELECT a AS b FROM x
"b" is just an alias name in the query. It does not create an extra column. Your result in the example would only contain one column named "b". But the column in the table would stay "a". "b" is just another name.
I don't really understand what you mean with "so I can use it with each item later on". Do you mean later in the select statement or later in your application. Perhaps you could provide some example code.
You could use a CASE statement, like
SELECT name
,address
,CASE WHEN a < b THEN '1'
ELSE '2' END AS one_or_two
FROM ...
You could use a CASE statement, like
SELECT name
,address
,CASE WHEN a < b THEN '1'
ELSE '2' END AS one_or_two
FROM ...
Your syntax would create an alias for a as b, but it wouldn't have scope beyond the results of the statement. It sounds like you may want to create a VIEW
Try this one if you want to create a virtual column "age" within a select statement:
select brand, name, "10" as age from cars...
You can add virtual columns as
SELECT '1' as temp
But if you tries to put where condition to additionally generated column, it wont work and will show an error message as the column doesn't exist.
We can solve this issue by returning sql result as a table.ie,
SELECT tb.* from (SELECT 1 as temp) as tb WHERE tb.temp = 1
Your syntax would create an alias for a as b, but it wouldn't have scope beyond the results of the statement. It sounds like you may want to create a VIEW
SELECT only retrieves data from the database, it does not change the table itself.
If you write
SELECT a AS b FROM x
"b" is just an alias name in the query. It does not create an extra column. Your result in the example would only contain one column named "b". But the column in the table would stay "a". "b" is just another name.
I don't really understand what you mean with "so I can use it with each item later on". Do you mean later in the select statement or later in your application. Perhaps you could provide some example code.
Try this one if you want to create a virtual column "age" within a select statement:
select brand, name, "10" as age from cars...
You can add virtual columns as
SELECT '1' as temp
But if you tries to put where condition to additionally generated column, it wont work and will show an error message as the column doesn't exist.
We can solve this issue by returning sql result as a table.ie,
SELECT tb.* from (SELECT 1 as temp) as tb WHERE tb.temp = 1
Your syntax would create an alias for a as b, but it wouldn't have scope beyond the results of the statement. It sounds like you may want to create a VIEW
SELECT only retrieves data from the database, it does not change the table itself.
If you write
SELECT a AS b FROM x
"b" is just an alias name in the query. It does not create an extra column. Your result in the example would only contain one column named "b". But the column in the table would stay "a". "b" is just another name.
I don't really understand what you mean with "so I can use it with each item later on". Do you mean later in the select statement or later in your application. Perhaps you could provide some example code.
Source: Stackoverflow.com