views:

81

answers:

1

I have a couple joins that I'm doing. I say tablename.column to identify what I want to select in the database...don't want to select all columns. However, my last join I do want to select all for that. Is there a way I can use an asterisk or something for the last join?

The last table is going to be dynamic too so I can't hard code it in (although I could write it out dynamically) but thought there might be an easier way.

    SELECT content_name.name,
           house.listing,
           street.* 
      FROM content 
INNER JOIN house ON content_name.id=house.id 
 LEFT JOIN street ON content_name.id=street.id;
+2  A: 

Alias your last table the same way everytime, and then just .* your alias.

SELECT content_name.name,house.listing, last_table.* 
FROM content INNER JOIN house ON 
content_name.id=house.id 
LEFT JOIN street last_table ON content_name.id=last_table.id;

That being said, * in a production query is an accident waiting to happen.

rfusca
+1: It's the part about the "last table is going to be dynamic" that freaks me out...
OMG Ponies
dynamic in the sense I control it in my php...nothing comes from the user.
Keith
Keith: If you're seperating out users/customers into different tables, I would consider a common table name in different schemas and then just set your "search_path" depending on where they need to look. It should make alot of those kind of dynamic queries simpler.
rfusca