我有两张这样的表: -
表A
CODE DESC
---- ----
A001 BLACK
12AA SOFT
B001 GREY
13SW ANTIQUE
B011 BROWN
23WQ HARD
表B
NO MODEL COLOR FINISH
-- ----- ----- ------
01 QWER A001 12AA
02 ASDF B011 13SW
03 ZXCV B001 12AA
04 POIU A001 23WQ
我希望使用SQL获得这样的输出: -
NO MODEL COLOR FINISH
-- ----- ----- ------
01 QWER BLACK SOFT
02 ASDF BROWN ANTIQUE
03 ZXCV GREY SOFT
04 POIU BLACK HARD
答案 0 :(得分:2)
您需要针对table_a
加入两次:
select b.no, b.model, a1.desc, a2.desc
from table_b b
inner join table_a a1 on (b.color = a1.code)
inner join table_a a2 on (b.finish = a2.code)
答案 1 :(得分:2)
研究SQL join
s:
select b.no, b.model, a1.desc as 'color', a.desc as 'finish'
from tableb b
inner join tablea as a
on b.finish = a.code
inner join tabla as a1
on b.color = a1.code
此查询根据公共标识符(在此code
中)“合并”两个表。
答案 2 :(得分:0)
你在这里:)
SELECT TEMP1.NO,TEMP1.MODEL,TEMP1.COLOR,TEMP2.FINISH FROM
(SELECT B.NO, B.MODEL, A.CODE,A.DESC AS COLOR FROM A,B WHERE A.CODE=B.COLOR)TEMP1,
(SELECT B.NO, B.MODEL, A.CODE,A.DESC AS FINISH FROM A,B WHERE A.CODE=B.FINISH)TEMP2
WHERE TEMP1.NO=TEMP2.NO