sqlite – 从同一个表多个INNER JOIN

前端之家收集整理的这篇文章主要介绍了sqlite – 从同一个表多个INNER JOIN前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一张金属桌子
MetalID    integer
MetalName  text
MetalCode  text

物品表

ItemID     integer
ItemName   text
...
Metal1     int Ref.-> Metals.MetalID
Metal2     int Ref.-> Metals.MetalID
Metal3     int Ref.-> Metals.MetalID

我试图选择三个MetalCodes

SELECT m.MetalCode as 'Metal1',m.MetalCode as 'Metal2',m.MetalCode as 'Metal3'
FROM Item as k
INNER JOIN Metals AS m ON m.MetalID=k.Metal1 
INNER JOIN Metals AS m ON m.MetalID=k.Metal2
INNER JOIN Metals AS m ON m.MetalID=k.Metal3
WHERE k.ItemID=?

看起来我完全错了.请帮忙.

您应该为表指定多个别名.你正在打电话给他们.
SELECT m1.MetalCode as 'Metal1',m2.MetalCode as 'Metal2',m3.MetalCode as 'Metal3'
FROM Item as k
INNER JOIN Metals AS m1 ON m1.MetalID=k.Metal1 
INNER JOIN Metals AS m2 ON m2.MetalID=k.Metal2
INNER JOIN Metals AS m3 ON m3.MetalID=k.Metal3
WHERE k.ItemID=?

猜你在找的Sqlite相关文章