views:

852

answers:

5

Hi,

I tried doing this but it failed.

SELECT table2.ID, table1.* FROM table2
LEFT JOIN table1 ON table1.ID = table2.table1ID

How do you select all columns from a table?

EDIT: There is no error in the above query. I don't know what caused the error but the code is now working.

A: 

What was the error message? Are you running on SQL Server?

Jonathan Parker
+3  A: 

You had field names conflict as both tables have ID field. You must to

 SELECT table2.ID as t2_id, table1.* FROM table2
LEFT JOIN table1 ON table1.ID = table2.table1ID
Riho
A field name conflict won't prevent the query from running.
Tomalak
It was not said how it failed
Riho
+1  A: 
    SELECT t2.ID, t1.* FROM table2 t2
LEFT JOIN table1 t1 ON t1.ID = t2.table1ID

this works on sql 2000+

Oscar Cabrero
+2  A: 

What you have is syntactically correct, exactly what did you mean by it failed? Did you get an error message or just not the results you wanted? (BTW it is a bad practice to select *, only return the columns you need. In this case you do not need all the columns as the id field in table1 will have the exact same data as the file din table 2 it is joined to)

HLGEM
Thanks HLGEM! Why is it bad to select *? What if I have 20 columns and I need 18 of them? Thanks.
Select * should not ever be used in production servers as it sends more infomation than you need across the network which creates performance problems over time. Further, if you change the structure of the table, select * gives the wrong results, so suddenly your report that is supposed to have ten columns has eleven. And if someone foolishly changed the order of the columns in the table, you may have the wrong data in the wrong column.
HLGEM
A: 

Sorry everyone, now that I run it again there is no error.
I'm not sure why I got an error last night, I thought it was because of "table1.*"

HLGEM is right there is no error with the query.