에서 두 열의 ID 값을 기반으로 table1
해당 항목을 가져올 수 table2
있습니까?
내 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
그러나 한 테이블에서 값을 삭제하면 다른 테이블이 해당 행을 계속 인덱싱하므로 정의되지 않은 오프셋 문제가 있습니다.
두 번째 테이블을 두 번 조인 할 수 있습니다.
SELECT t1.id,
t1.origin,
t1.destiny,
o.name as nameOrigin,
d.name as nameDestiny
FROM table1 t1
INNER JOIN table2 o ON t1.origin = o.id
INNER JOIN table2 d ON t1.destiny = d.id
주의 : "운명" 은 "목적지" 와는 다릅니다 .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다