Page 1 of 1
Select all columns from two tables
Posted: Sun Apr 29, 2007 4:43 pm
by SmokyBarnable
Is there an easy way to select all columns from one table and just one column from a different table?
Also, what happens when you select * from two tables that have column names that are identical?
Posted: Sun Apr 29, 2007 5:04 pm
by John Cartwright
You specifiy which tables you want to use using the FROM clause, or by performing a JOIN. If two columns have identical names, one of the values is overwritten (you'll have to look up which one because I have forgotten), therefor if you want to keep both column names you'll need to use an alias
Code: Select all
SELECT table1.id AS id1, table2.id AS id2
Re: Select all columns from two tables
Posted: Wed May 02, 2007 3:07 pm
by califdon
SmokyBarnable wrote:Is there an easy way to select all columns from one table and just one column from a different table?
Also, what happens when you select * from two tables that have column names that are identical?
Better be careful here or you'll end up with a Cartesian Join, joining every row in one table with every row in the other table, almost certainly not what you want. But if you understand that, the syntax is quite simple:
Code: Select all
SELECT a.*, b.somecol FROM table1 AS a, table2 AS b WHERE a.foreignkey = b.primarykey