Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select * with specific alias [syntax]

Tags:

sql

I want to use select * to select all the fields from a table but I also want to use an alias on just one field. Is it possible? If so what would the syntax be.

Example

select *,
item1 as hats
from factory

I have not been able to make anything like this work, Thanks!

like image 358
tylercomp Avatar asked Jun 16 '11 23:06

tylercomp


People also ask

How do I use aliases in SELECT?

Alias is used to give a temporary name(only for the duration of the query) to the column or table in order to make the column name or table name more readable. It does not change the name of the column permanently. Alias can be performed using the 'AS' keyword or without any keyword.

How do you specify an alias in SQL?

The basic syntax of a table alias is as follows. SELECT column1, column2.... FROM table_name AS alias_name WHERE [condition];

Can alias Use same SELECT statement?

No there isn't a way to refer to aliases, but you can assign the expression to a variable, and then refer to the variable in the same select clause. Inside a select statement variable assignment is always done by the infix operator := .


1 Answers

I just tried

select *, item1 as hats from factory

on mysql and postgres and on both DBMS it runs fine. Only take into consideration that you get two columns with item1. One column named item1 and one named hats.

like image 167
Hyperboreus Avatar answered Nov 15 '22 19:11

Hyperboreus