1

假设我有一张这样的桌子:

+----------+-----------+-----------+
|   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 做一些事情,但我真的不知道如何使它工作。

4

1 回答 1

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
于 2013-11-01T22:20:07.520 回答