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!
I just tried
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.
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.
Select *
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).
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.
MS Sql Server
select *,col2 as 'customcol' from table1