我在查询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
有人可以帮助我。
非常感谢
答案 0 :(得分: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
答案 1 :(得分:0)
您需要以下内容:
select car,
(select description from table2 where ID = table1.brand),
(select description from table2 where ID = table1.model)
from table1
答案 2 :(得分: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;