tags:

views:

1136

answers:

3

is there a way to have a mysql select statement return fully qualified column names like "table.field" without using AS for every single field?

like so:

SELECT * 
FROM  table1  
LEFT JOIN table2 on table1.f_ID = table2.ID

and the result would be: "table1.ID", "table1.name", "table2.ID", "table2.name", ...

+1  A: 

Not really. You could write some dynamic SQL to accomplish this, but it wouldn't be simple. If you really want the dynamic SQL, let me know and I could try to whip something up.

A: 

Using fully qualified field names in the statement would yield fully qualified field names in the result set.

SELECT table1.ID, table1.name, table2.ID, table2.name, FROM table1 LEFT JOIN table2 on table1.f_ID = table2.ID

I realize that the burden you wished to get rid of is doing this manually. But if your application has any kind of knowledge about the layout of the database, it can be generated dynamically within the application.

Just my 2 cents

Mihai Stancu
A: 

You can write this as well:

   SELECT table1.*, table2.*
     FROM table1 
LEFT JOIN table2 on table1.f_ID = table2.ID

No need to specify each column, if you want them all

I tried this on a SQLite3 database and it didn't fully qualify the column names. Although the OP asked about MySQL, I thought it would be worth noting.
Benjamin Oakes