user2772069
user2772069

Reputation: 1

SQL Query - joining tables

I'm quite new in query SQL and i need some help. I have information on 2 tables:

Table 1:

Car# | Brand (ID) | Model (ID)
123  |      23    |     47

Table 2:

ID  | Description
23  |  Honda
47  |  Civic  

And I need to perform a select that give the output:

Car# | Brand (ID) | Model (ID)
123  |   Honda    |   Civic

Can someone help me.

Many thanks

Upvotes: 0

Views: 57

Answers (3)

Srini V
Srini V

Reputation: 11355

Try this

WITH TABLE1 AS (SELECT '123' AS CAR, '23' AS BRAND, '47' AS MODEL FROM DUAL), TABLE2 AS (SELECT '23' AS ID, 'Honda' AS DESCRIPTION FROM DUAL UNION ALL SELECT '47' AS ID, 'Civic' AS DESCRIPTION FROM DUAL) SELECT A.CAR, B.DESCRIPTION, C.DESCRIPTION FROM TABLE1 A, (SELECT DESCRIPTION FROM TABLE2, TABLE1 WHERE BRAND = ID) B, (SELECT DESCRIPTION FROM TABLE2, TABLE1 WHERE MODEL = ID) C;

Upvotes: 0

mhafellner
mhafellner

Reputation: 468

You would need something like:

select car, 
(select description from table2 where ID = table1.brand), 
(select description from table2 where ID = table1.model) 
from table1

Upvotes: 0

Robert
Robert

Reputation: 25753

You should join to table2 twice as below

select t1.car, t2.Description as brand,t3.Description as model
from table1 t1
join table2 t2 on t2.id = t1.brand
join table2 t3 on t3.id = t1.model

Upvotes: 1

Related Questions