我有兩個表。表1存儲代碼的位置順序。表2存儲每個代碼的信息。我試圖做一個查詢完成一個ID,返回每個代碼及其相關信息的順序。SQL按照順序連接兩個表
TABLE1
+-------+-----------+----------+-----------+-----------+-----------+
| id | position1 | positon2 | position3 | position4 | position5 |
+-------+-----------+----------+-----------+-----------+-----------+
| id1 | AA | CC | BB | EE | DD |
|-------|-----------|----------|-----------|-----------|-----------|
| id2 | BB | AA | DD | EE | CC |
+-------+-----------+----------+-----------+-----------+-----------+
TABLE2
+------+------------+--------------------+
| code | name | active_icon |
+------+------------+--------------------+
| AA | nameA | iconA |
| BB | nameB | iconB |
| CC | nameC | iconC |
| DD | nameD | iconD |
| EE | nameE | iconE |
+------+------------+--------------------+
做了一個ID(例如ID = ID2),我期待的結果是:
+----------------+------+------------+--------------------+
| position | code | name | active_icon |
+----------------+------+------------+--------------------+
| 1 | BB | nameB | iconB |
| 2 | AA | nameA | iconA |
| 3 | DD | nameD | iconD |
| 4 | EE | nameE | iconE |
| 5 | CC | nameC | iconC |
+----------------+------+------------+--------------------+
任何想法?我認爲必須有一個INNER JOIN,但我不知道如何去解決它。
如果將表1歸一化,會更好。 – Barmar
與表1幾乎不可能 – nidomiro