Amber O
Amber O

Reputation: 67

To use SQL join or not

I have a query with 2 tables Table A and Table B.

I want value of column A3 from Table_A and B2 from Table_B.

I tried this query:

select 
    b.B2, a.A3 
from 
    Table_A a 
join 
    Table_B b on (a.A_ID = b.B_ID)

The query returns nothing. What am I doing wrong?

Upvotes: 1

Views: 68

Answers (1)

marc_s
marc_s

Reputation: 754598

It would seem that the B_ID column in Table A is the foreign key to Table B - in that case, you need to join on that column:

select 
    b.B2, a.A3 
from 
    Table_A a 
inner join 
    Table_B b on a.B_ID = b.B_ID

Use a.B_ID (not a.A_ID) to establish the join between the two tables.

Upvotes: 2

Related Questions