根據table1
中兩列的ID值,可以得到它們在table2
中的等效值嗎?用於比較兩列與另一個表中的一列(並獲取兩個值)的SQL查詢
我table1
看起來是這樣的:
id | origin | destiny
-- | ------ | -------
1 | 2 | 3
2 | 4 | 5
和table2
,像這樣:
id | name
-- | ----
1 | bla
2 | asd
3 | dfg
4 | qwe
5 | tle
我想是這樣的:
id | origin | destiny | nameOrigin | nameDestiny
-- | ------ | ------- | ---------- | -----------
1 | 2 | 3 | asd | dfg
2 | 4 | 5 | qwe | tle
我試着做兩個查詢:
SELECT
t1.origin,
t1.destiny,
t2.name
FROM
table1 t1
INNER JOIN table2 t2 ON t1.origin = t2.id
和:
SELECT
t1.origin,
t1.destiny,
t2.name as destinyName
FROM
table1 t1
INNER JOIN table2 t2 ON t1.destiny = t2.id
但是,如果我從一個表中刪除一個值時,另一個保持索引該行因此有一個未定義的彌補問題。
謝謝你的快速回答:) – szk42