使用单个Query加入3个表

时间:2017-10-17 06:49:15

标签: mysql sql

我有3个表,零售商,产品和制造商。我会将retailerID传递给查询,它应该返回制造商的详细信息。

criteria:
1)from the given input (retailerID) fetch productID from the retailer table.
2)Using productID get the manufacturerID from product table.
3)Get the complete details of manufacturer from manufacturer table using manufacturerID.

任何人都可以帮我解决这个问题。谢谢。

1 个答案:

答案 0 :(得分:0)

根据您对问题的描述,我了解这是您的要求:

select * 
from manufacturer 
where manufacturerID = (
     select manufacturerID 
     from Product 
     where productID = (
         select productID 
         from retailer 
         where retailerID=@retailerID)
     )

以上查询以子查询格式编写,可以很容易地转换为连接格式:

select * 
from Manufacturer m 
join Product p on m.manufacturerID = p.manufacturerID 
join retailer r on r.ProductID = p.ProductID
where r.RetailerID = @retailerID