2013-04-05 90 views
-1

我有2個表使用連接如何比較2列

表1

ID Name 
1 OS 
2 Harddisk 
3 RAM 
4 WINDOWS 
5 LINUX 
6 SOLARIS 
7 MAC 
8 UNIX 
9 DCCI 

表2

ID Table1_ID Table1_component 
1 1   4 
2 1   5 
3 1   6 
4 1   7 
5 1   8 
6 1   9 

我想加入上述2個表和我需要輸出爲

Table1_ID Table1_component 
    OS   Windows 
    OS   Linux 
    OS   SOLARIS 
    OS   MAC 
    OS   UNIX 
    OS   DCCI 

請幫助我,而不是數字在Table 2我需要Table1

+2

請張貼什麼你嘗試過的代碼並解釋它爲什麼不起作用。 – 2013-04-05 02:44:46

回答

1

名字你需要加入對Table1兩次,得到的結果:

select t1.name as table1_id, 
    c.name as Table1_component 
from table1 t1 
inner join table2 t2 
    on t1.id = t2.table1_id 
inner join table1 c 
    on t2.Table1_component = c.id 

SQL Fiddle with Demo

+0

謝謝你......它的工作 – user2247355 2013-04-05 03:35:56