I have two tables like this:-
TABLE A
CODE DESC
---- ----
A001 BLACK
12AA SOFT
B001 GREY
13SW ANTIQUE
B011 BROWN
23WQ HARD
TABLE B
NO MODEL COLOR FINISH
-- ----- ----- ------
01 QWER A001 12AA
02 ASDF B011 13SW
03 ZXCV B001 12AA
04 POIU A001 23WQ
I wish to hv an output like this using SQL:-
NO MODEL COLOR FINISH
-- ----- ----- ------
01 QWER BLACK SOFT
02 ASDF BROWN ANTIQUE开发者_运维百科
03 ZXCV GREY SOFT
04 POIU BLACK HARD
Research 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
This query 'merges' the two tables based on a common indentifier (in this the code
).
You need to join two times against 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)
Here you are :)
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
精彩评论