我有2个表,表A的2列包含ID,表B的ID与电影名称匹配。我想选择电影名称而不是电影ID(例如
)的行表A
+-----+----------------------------------------------+
| id | name | favmovieid| leastfavmovieid |
+-----+----------------------------------------------+
| 101 | Name 1 | 1 | 5 |
| 102 | Name 2 | 6 | 8 |
| 103 | Name 3 | 8 | 6 |
+-----+----------------------------------------------+
表B
+-------+---------------------+
| movid | namemovie |
+-------+---------------------+
| 1 | Harry Potter 1 |
| 2 | Harry Potter 2 |
| 3 | Harry Potter 3 |
| 4 | Lord of the Rings 1 |
| 5 | Lord of the Rings 2 |
| 6 | Lord of the Rings 3 |
| 7 | Iron Man 1 |
| 8 | Iron Man 2 |
+-------+---------------------+
我试图通过内部连接两个列(例如
)来使用内部连接Select TableB.namemovie,TableB.namemovie from
((TableA inner join TableB on TableA.favmovieid=TableB.movid) inner join on TableB TableA.leastfavmovieid=TableB.movid) where TableA.id=101
但是我收到一个错误,说表B被提及太多了
然后我还尝试了以
的身份进行联合Select TableB.namemovie from (TableA inner join TableB on TableA.favmovieid=TableB.movid)
where TableA.id=101
union
Select TableB.namemovie from (TableA inner join TableB on TableA.leastfavmovieid=TableB.movid)
where TableA.id=101
它确实返回了两个电影名称,但我只想返回一行,因为我希望在该行中返回更多信息,例如名称,价格或其他任何列。
答案 0 :(得分:0)
您接近了,窍门是使用别名在每个联接上引用表,这应该可行:
select b1.namemovie,b2.namemovie from TableA
inner join TableB b1 on TableA.favmovieid=b1.movid
inner join TableB b2 on TableA.leastfavmovieid=b2.movid
where TableA.id=101