Select * with a special alias [syntax]

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

Example

select *, item1 as hats from factory 

I could not do something like this work, thanks!

+7
source share
4 answers

I just tried

 select *, item1 as hats from factory 

on mysql and postgres, and on both DBMSs it works fine. Just bear in mind that you get two columns with item1. One column named item1 and one of the named hats.

+14
source

Yes, this format can be used, but using Select * not recommended. Better list the columns you need. Some database products may be hindered if the list of columns combined with your column alias creates a duplicate column name. However, again, you can solve this by listing the columns.

+2
source

This should work, but keep in mind that your column item1 will be duplicated!

Suppose your table has columns (id, item1, item2), then your suggested choice will return (id, item1, item2, hats).

+2
source

It is valid in MS Sql Server , but the column that you alias'ng will be duplicated. I tried the request in Sql server 2005 and it worked.

 select *,col2 as 'customcol' from table1 
+2
source

All Articles