【发布时间】:2017-09-05 11:18:15
【问题描述】:
是否有可能根据table1 中两列的ID 值,在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
但是如果我从一个表中删除一个值,另一个表会继续索引该行,因此存在未定义的偏移问题。
【问题讨论】:
标签: php mysql sql-server join