我有两张桌子。我试图加入下面两个表的样本,表1引用表2两次。例如,如果我查看表1:组2和成员7,它应该在表2中查找ID并给出一个输出:
Group Members Name Name
2 7 Blue Dog
表1
Group Members
2 7
3 8
5 10
表2
ID Name
1 Green
2 Blue
3 Yellow
4 Orange
5 Red
6 Elephant
7 Dog
8 Cat
9 Chicken
10 Mouse
任何建议?感谢
答案 0 :(得分:1)
SELECT
Table_1.*,
g.Name,
m.Name
FROM
Table_1
INNER JOIN Table_2 AS g ON Table_1.Group=g.ID
INNER JOIN Table_2 AS m ON Table_1.Group=m.ID
WHERE
Table_1.Group=2
AND Table_1.Member=7
答案 1 :(得分:0)
在不同的列上两次加入表2。
SELECT t1.*, t2.name AS group_name, t3.name AS member_name
FROM Table1 AS t1
JOIN Table2 AS t2 ON t1.group = t2.id
JOIN Table2 AS t3 ON t1.members = t3.id
答案 2 :(得分:0)
很难从该描述中确切地说出您需要的内容,但是表格的别名可能就是您所需要的。它的工作原理如下:
SELECT t1.x, t2_1.y, t2_2.z
FROM table1 AS t1
JOIN table2 AS t2_1 ON t1.whatever = t2_1.whatever
JOIN table2 AS t2_2 ON t1.whatever = t2_2.whatever
...