0

我有这样的值的表

TABLE1
ID....VALUE
-----------
1.....Hello
2.....World
3.....Other
4.....Words

还有一张带钥匙的桌子

TABLE2
ID...WORD1...WORD2...OTHER...FIELDS
-----------------------------------
1.....1.......1......NULL....NULL..
2.....1.......3......NULL....NULL..
3.....4.......1......NULL....NULL..

我想要做的是加入这两个,以便它产生这样的东西:

SELECT ... FROM `TABLE2` ...
=
Hello Hello
Hello Other
Words Hello

但我完全迷路了!

4

3 回答 3

2

这应该可以满足您的需求。

select t1a.word, t1b.word from 
TABLE2 t2
join table1 t1a on t2.word1 = t1a.ID
join table1 t1b on t2.word2 = t1b.ID

只要您确保表的每个实例都有唯一的别名,您就可以将表连接到原始表的次数不限。

于 2013-08-06T18:18:47.310 回答
1
SELECT t1a.value, t1b.value
FROM table2 
JOIN table1 as t1a on word1 = t1a.id
JOIN table1 as t1b on word2 = t1b.id;
于 2013-08-06T18:18:54.153 回答
0

尝试这个

   select t1.VALUE, t3.VALUE as value2 from 
   Table2 t2
   join Table1 t1 on t2.WORD1 = t1.ID
   join table1 t3 on t2.WORD2 = t3.ID

演示

于 2013-08-06T18:25:13.690 回答