#00AF33
Bark Different.
102833
0
1
Feb 12, 2023 16:57:46 GMT -8
RedBassett
I'm a Marxist/Lennonist of the Groucho/John variety.
15,405
April 2007
applecomputer
RedBassett's Mini-Profile
|
Post by RedBassett on Mar 20, 2012 15:03:41 GMT -8
Greetings,
Is there a way in SQL to say SELECT tablename.*, such that each column in the result is named tablename.columnname?
- RedBassett
|
|
inherit
40157
tyrantlytamale 627939549 tjhtmlmaniac
0
Sept 3, 2023 15:17:02 GMT -8
Tylr
The stale taste of recycled air.
2,964
April 2005
tyrantlytamale
|
Post by Tylr on Mar 20, 2012 19:41:21 GMT -8
You mean like this? SELECT * FROM `tablename` WHERE `name` = 'columnname'
If not, could you explain a little more what you're trying to do?
|
|
#00AF33
Bark Different.
102833
0
1
Feb 12, 2023 16:57:46 GMT -8
RedBassett
I'm a Marxist/Lennonist of the Groucho/John variety.
15,405
April 2007
applecomputer
RedBassett's Mini-Profile
|
Post by RedBassett on Mar 20, 2012 20:18:18 GMT -8
You mean like this? SELECT * FROM `tablename` WHERE `name` = 'columnname'
If not, could you explain a little more what you're trying to do? Apologies. Table has three columns. Column One | Column Two | Column Three |
If I say "SELECT * FROM table", it will return the three columns as above, however if I say "SELECT `column_one` AS `table.column_one` (for each column), it will return: Tablename.Column One | Tablenam.Column Two | Tablename.Column Three |
I am trying to find a way to do the second action, using a SELECT * command, instead of manually entering each alias.
|
|
inherit
40157
tyrantlytamale 627939549 tjhtmlmaniac
0
Sept 3, 2023 15:17:02 GMT -8
Tylr
The stale taste of recycled air.
2,964
April 2005
tyrantlytamale
|
Post by Tylr on Mar 20, 2012 20:36:55 GMT -8
Ah, I think I see what you're saying. What RDMS are you using? In MySQL, SELECT * automatically returns an array, so you don't have to worry about that. (Instead of Tablename.ColumnOne you'd get tablename['columnone'])
|
|
#00AF33
Bark Different.
102833
0
1
Feb 12, 2023 16:57:46 GMT -8
RedBassett
I'm a Marxist/Lennonist of the Groucho/John variety.
15,405
April 2007
applecomputer
RedBassett's Mini-Profile
|
Post by RedBassett on Mar 20, 2012 21:34:15 GMT -8
Ah, I think I see what you're saying. What RDMS are you using? In MySQL, SELECT * automatically returns an array, so you don't have to worry about that. (Instead of Tablename.ColumnOne you'd get tablename['columnone']) My issue is when I start using multiple tables. I need to label the values with their original tables.
|
|
inherit
16846
0
Nov 19, 2012 15:20:20 GMT -8
Chris
3,036
December 2003
cddude
|
Post by Chris on Mar 25, 2012 10:02:15 GMT -8
Ah, I think I see what you're saying. What RDMS are you using? In MySQL, SELECT * automatically returns an array, so you don't have to worry about that. (Instead of Tablename.ColumnOne you'd get tablename['columnone']) My issue is when I start using multiple tables. I need to label the values with their original tables. I don't know an automatic way, but you can manually do that... SELECT column AS super_awesome_column FROM table1
|
|
#00AF33
Bark Different.
102833
0
1
Feb 12, 2023 16:57:46 GMT -8
RedBassett
I'm a Marxist/Lennonist of the Groucho/John variety.
15,405
April 2007
applecomputer
RedBassett's Mini-Profile
|
Post by RedBassett on Mar 25, 2012 16:37:59 GMT -8
My issue is when I start using multiple tables. I need to label the values with their original tables. I don't know an automatic way, but you can manually do that... SELECT column AS super_awesome_column FROM table1 I have been aliasing the columns manually so far, but I just wondered if there was a way to do prepend the table name without typing in each column name. Worst comes to worst, I can always deal with long SQL queries. Probably going to have the names generated by code anyway. - RedBassett
|
|
inherit
50924
0
Feb 28, 2021 14:59:32 GMT -8
BFD
Last place must get awful crowded at the Nice Guy Olympics
1,708
July 2005
dwinman164
|
Post by BFD on Mar 27, 2012 15:02:25 GMT -8
I don't know an automatic way, but you can manually do that... SELECT column AS super_awesome_column FROM table1 I have been aliasing the columns manually so far, but I just wondered if there was a way to do prepend the table name without typing in each column name. Worst comes to worst, I can always deal with long SQL queries. Probably going to have the names generated by code anyway. - RedBassett As a DBA, I am supposed to tell you to avoid using SELECT *, especially when accessing from code. Database servers run much better if they are only asked to return the columns that are needed. Experience says that SELECT * isn't all that evil - unless you are dealing with serverside objects (stored procedures, views, etc.). To directly answer the question...I do not know of any DBMS (other than maybe Microsoft Access) where you can have a query automatically return an alias for individual columns. Building the query in code is the only way I know of.
|
|
#00AF33
Bark Different.
102833
0
1
Feb 12, 2023 16:57:46 GMT -8
RedBassett
I'm a Marxist/Lennonist of the Groucho/John variety.
15,405
April 2007
applecomputer
RedBassett's Mini-Profile
|
Post by RedBassett on Mar 27, 2012 15:06:47 GMT -8
I have been aliasing the columns manually so far, but I just wondered if there was a way to do prepend the table name without typing in each column name. Worst comes to worst, I can always deal with long SQL queries. Probably going to have the names generated by code anyway. - RedBassett As a DBA, I am supposed to tell you to avoid using SELECT *, especially when accessing from code. Database servers run much better if they are only asked to return the columns that are needed. Experience says that SELECT * isn't all that evil - unless you are dealing with serverside objects (stored procedures, views, etc.). To directly answer the question...I do not know of any DBMS (other than maybe Microsoft Access) where you can have a query automatically return an alias for individual columns. Building the query in code is the only way I know of. Thank you. As for the SELECT *, I have code that has a number of objects, each representing a website object (such as a user account). The SELECT * is to load the table from the DB into that object. - RedBassett
|
|
inherit
97216
0
Nov 23, 2024 12:51:52 GMT -8
Bennett 🚀
Formerly iPokemon.
3,622
January 2007
catattack
iPokemon's Mini-Profile
|
Post by Bennett 🚀 on Mar 27, 2012 17:34:47 GMT -8
SELECT * is a very lazy way of getting stuff from a database and is a bad habit to get into. I myself do this all the time, but you should always select only what you need to save one load times & query time.
|
|
#00AF33
Bark Different.
102833
0
1
Feb 12, 2023 16:57:46 GMT -8
RedBassett
I'm a Marxist/Lennonist of the Groucho/John variety.
15,405
April 2007
applecomputer
RedBassett's Mini-Profile
|
Post by RedBassett on Mar 27, 2012 18:17:35 GMT -8
SELECT * is a very lazy way of getting stuff from a database and is a bad habit to get into. I myself do this all the time, but you should always select only what you need to save one load times & query time. Ok. As of this aliasing issue, I have been forced to start selecting everything manually. I actually updated the aliasing thing, so I don't have to use it in my code, I can do a standard select, but I will be forcing myself to use column names from here on out.
|
|
inherit
40157
tyrantlytamale 627939549 tjhtmlmaniac
0
Sept 3, 2023 15:17:02 GMT -8
Tylr
The stale taste of recycled air.
2,964
April 2005
tyrantlytamale
|
Post by Tylr on Mar 27, 2012 20:23:06 GMT -8
SELECT * is a very lazy way of getting stuff from a database and is a bad habit to get into. I myself do this all the time, but you should always select only what you need to save one load times & query time. How about if your table has ~1000 rows and ~10 columns? At what point does selecting specific columns actually make a difference?
|
|
inherit
50924
0
Feb 28, 2021 14:59:32 GMT -8
BFD
Last place must get awful crowded at the Nice Guy Olympics
1,708
July 2005
dwinman164
|
Post by BFD on Mar 28, 2012 7:37:38 GMT -8
SELECT * is a very lazy way of getting stuff from a database and is a bad habit to get into. I myself do this all the time, but you should always select only what you need to save one load times & query time. How about if your table has ~1000 rows and ~10 columns? At what point does selecting specific columns actually make a difference? EDIT TO DIRECTLY ANSWER:It really depends on the data type and the contents. Select * really hurts in SQL Server when you have a Text or Binary column that is used to store large amounts of data. I have seen where removing a single binary column from a query selecting a few hundred rows reduces execution from ten minutes plus to almost instant. And if you are returning your data as xml, even columns with small amounts of data can really bloat your return results. Aliasing 10 columns is not that difficult...and if you always alias, then you don't have field name conflicts when you join in additional tables (foreign key column names should match the primary key column name whenever possible). A good rule of thumb is to always alias column names when you are joining multiple tables...and if a single table has enough columns to where aliasing gets cumbersome, you should consider further normalization.
|
|