akosch
akosch

Reputation: 4406

How do I join two tables ON a column, that has the same name in both tables?

see above...

Upvotes: 1

Views: 269

Answers (4)

Garry Shutler
Garry Shutler

Reputation: 32698

select  *
from    Table1
        inner join
        Table2
        on Table1.ColumnName = Table2.ColumnName

Simple really.

Upvotes: 4

Milen A. Radev
Milen A. Radev

Reputation: 62593

And for completeness (depending on your DBMS) you could use "USING":

SELECT
    ...
FROM
    table_a
INNER JOIN
    table_b
USING
    (common_column);

Upvotes: 0

StingyJack
StingyJack

Reputation: 19479

Use an Alias for the table names is the shortest.

SELECT a.*, b.*
FROM table1 as 'a'
  INNER JOIN table2 as 'b'
    ON a.col1 = b.col1

You can also specify the full table names.

SELECT table1.*, table2.*
FROM table1
  INNER JOIN table2 
    ON table1.col1 = table2.col1

Upvotes: 8

Mehrdad Afshari
Mehrdad Afshari

Reputation: 422046

What you are asking is called NATURAL JOIN in relational terminology. Some database servers support this clause. I would prefer to manually specify the join expression even if the provider supports such a clause like:

SELECT .... FROM Table1 JOIN Table2 ON Table1.JoinCol = Table2.JoinCol ...

Upvotes: 5

Related Questions