0

我是查询 SQL 的新手,我需要一些帮助。我有2张桌子的信息:

表格1:

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

表 2:

ID  | Description
23  |  Honda
47  |  Civic  

我需要执行一个提供输出的选择:

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

有人能帮我吗。

非常感谢

4

3 回答 3

1

您应该按照以下方式加入 table2 两次

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
于 2013-09-12T10:00:28.240 回答
0

你需要类似的东西:

select car, 
(select description from table2 where ID = table1.brand), 
(select description from table2 where ID = table1.model) 
from table1
于 2013-09-12T10:03:18.257 回答
0

尝试这个

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;

于 2013-09-12T10:08:22.660 回答