我正在尝试使用TWICE内部连接语句来获取相同详细信息表中的引用值。
Master table: bags_tbl
ID ... bagA bagB
1 ... 121 122
2 ... 123 124
3 ... 125 126
Detail table: fruit_tbl
ID ... fruit ...
121 strawbery
122 apple
123 orange
124 raspberry
125 pear
126 pineapple
SELECT
bags_tbl.ID,
bags_tbl.A,
bags_tbl.B,
fruit_tbl.fruit AS bagA_fruit,
fruit_tbl.fruit AS bagB_fruit
FROM
bags_tbl
Inner Join fruit_tbl ON bags_tbl.bagA = fruit_tbl.fruit
Inner Join fruit_tbl ON bags_tbl.bagB = fruit_tbl.fruit
这个抛出错误:没有唯一的表/别名...... 如何使SQL语句获取主表的文本表示?
非常感谢
答案 0 :(得分:3)
SELECT
bags_tbl.ID,
bags_tbl.A,
bags_tbl.B,
A.fruit AS bagA_fruit,
B.fruit AS bagB_fruit
FROM bags_tbl
Inner Join fruit_tbl A ON bags_tbl.bagA = A.id
Inner Join fruit_tbl B ON bags_tbl.bagB = B.id
答案 1 :(得分:1)
试试这个。您需要为每个连接授予一个唯一的别名,以便SQL知道您在SELECT子句中引用了哪一个。
SELECT
bags_tbl.ID,
bags_tbl.A,
bags_tbl.B,
fruitA.fruit AS bagA_fruit,
fruitB.fruit AS bagB_fruit
FROM
bags_tbl
JOIN fruit_tbl fruitA ON bags_tbl.bagA = fruitA.id
JOIN fruit_tbl fruitB ON bags_tbl.bagB = fruitB.id