Mysql加入并获得2个值?

时间:2013-08-06 18:15:15

标签: mysql

我的表格有这样的值

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

但我完全失去了!

3 个答案:

答案 0 :(得分: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

只要确保为表的每个实例都有唯一的别名,就可以根据需要多次连接表。

答案 1 :(得分:1)

SELECT t1a.value, t1b.value
FROM table2 
JOIN table1 as t1a on word1 = t1a.id
JOIN table1 as t1b on word2 = t1b.id;

答案 2 :(得分: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

DEMO