Jesper
Jesper

Reputation: 2094

How do I join two tables with same column names keeping values from one the tables

I have these two tables:

Table1
-------
id|a|b|c|d|
1 |0|1|0|6|

and

Table2
-------
id|a|c|
1 |3|2|

How do I join these two tables keeping the values from table2 but also the columns from table1, so that the table would look like this afterwards:

TableJoined
-------
id|a|b|c|d|
1 |3|1|2|6|

Tried with

SELECT * FROM Table2 a JOIN Table1 b WHERE a.id = b.id;

hoping that the first table mentioned would be the overrider

Or I guess you could do:

SELECT b.id, a.a, b.b, a.c, b.d FROM Table2 a JOIN Table1 b WHERE a.id = b.id;

Upvotes: 1

Views: 323

Answers (1)

Laurenz Albe
Laurenz Albe

Reputation: 247225

You can achieve that by qualifying the * with the table alias:

SELECT a.* FROM Table2 a JOIN Table1 b WHERE a.id = b.id;

But you should never use * in a SELECT list except in ad-hoc queries. (The exception is count(*) which is OK to use).

Upvotes: 1

Related Questions