dbTalk Databases Forums  

Can I do something like SELECT a.* as prefix.*

comp.databases comp.databases


Discuss Can I do something like SELECT a.* as prefix.* in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
AT
 
Posts: n/a

Default Can I do something like SELECT a.* as prefix.* - 06-12-2006 , 06:21 PM







In mysql 5.1, you can this:

select a.*, b.title as btitle from tablename a inner join tablename b
on a.bid = b.id;

but what I want to do is make a prefix name for every column that comes
back from the b table, like:

select a.*, b.* as btable.* from tablename a inner join tablename b on
a.bid = b.id;

So that, in the results, everything that comes back from the b table is
in columns names like like:

btable.title | btable.price | btable.description

Can I do that? I'm kind of suprised if I can't, actally.. Could I do
that in postgre?


Reply With Quote
  #2  
Old   
Bill Karwin
 
Posts: n/a

Default Re: Can I do something like SELECT a.* as prefix.* - 06-13-2006 , 12:10 PM






dterrors (AT) hotmail (DOT) com wrote:
Quote:
In mysql 5.1, you can this:

select a.*, b.title as btitle from tablename a inner join tablename b
on a.bid = b.id;

but what I want to do is make a prefix name for every column that comes
back from the b table, like:

select a.*, b.* as btable.* from tablename a inner join tablename b on
a.bid = b.id;

So that, in the results, everything that comes back from the b table is
in columns names like like:

btable.title | btable.price | btable.description

Can I do that? I'm kind of suprised if I can't, actally..
No, you can't. It isn't part of the SQL standard. You need to give
column aliases one at a time.

By the way, MySQL doesn't return the table name (nor the correlation
name) in the column label of the result set anyway. You'd get column
labels of "title", "price", "description", not "btable.title",
"btable.price", "btable.description".

You need to alias them individually, and put the label in delimiters
because it contains a dot character:

SELECT ...
btable.title AS `btable.title`,
btable.price AS `btable.price`,
btable.description AS `btable.description`, ...

For what it's worth, I never use * or table.* in my queries. Doing that
hides errors, if I ever change the table structure. For instance, if I
change the name of a column, I'd like the SQL query to give me an error,
as specific as possible.

If I were to use * wildcards, no error would be given, but the column
wouldn't exist in the result set by the name I expect. I typically
fetch values by column name from a row of the result set, so I'm likely
to simply get blanks back. Then it's more troublesome to figure out why
I have blanks in my web page where I should have data.

Quote:
Could I do that in postgre?
http://www.postgresql.org/docs/8.1/i...ql-select.html says:

SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]
* | expression [ AS output_name ] [, ...]

This doesn't appear to support column aliasing for wildcards like you
describe. In fact, based on that syntax description, it doesn't appear
to support b.* at all.

Regards,
Bill K.


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.