2013-11-01 99 views
0

可以說我有這樣的一個表:如何用另一個表中的相應值替換一個表中的ID?

+----------+-----------+-----------+ 
| name | color_id | shape_id | 
+----------+-----------+-----------+ 
| A  |  1  |  1  | 
+----------+-----------+-----------+ 
| B  |  2  |  2  | 
+----------+-----------+-----------+ 
| C  |  3  |  3  | 
+----------+-----------+-----------+ 

和兩個其他表像這樣:

+----------+-------+ 
| color_id | color | 
+----------+-------+ 
| 1  | R | 
+----------+-------+ 
| 2  | G | 
+----------+-------+ 
| 3  | B | 
+----------+-------+ 

+----------+-------+ 
| shape_id | shape | 
+----------+-------+ 
| 1  | S | 
+----------+-------+ 
| 2  | T | 
+----------+-------+ 
| 3  | C | 
+----------+-------+ 

我希望做一個查詢並得到這個結果集:

+----------+-----------+-----------+ 
| name | color | shape | 
+----------+-----------+-----------+ 
| A  |  R  |  S  | 
+----------+-----------+-----------+ 
| B  |  G  |  T  | 
+----------+-----------+-----------+ 
| C  |  B  |  C  | 
+----------+-----------+-----------+ 

我認爲它必須做一些JOIN,但我真的不知道如何使它工作。

回答

2
SELECT 
    name, color, shape 
FROM 
    table1 
     INNER JOIN 
     table2 ON table1.color_id = table2.color_id 
     INNER JOIN 
     table3 ON table1.shape_id = table3.shape_id 
+0

謝謝你,那是解決方案,現在我明白了所有那些加入維恩圖,謝謝! – arielnmz

+0

我不希望你的表格很大..沒有WHERE語句的連接會迫使MySQL引擎返回很多記錄.. –

相關問題