2017-10-17 37 views
-3

我有3個表格,零售商,產品和製造商。我將零售商ID傳遞給查詢,它應該返回製造商詳細信息。使用單個查詢加入3個表格

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. 

有誰能幫我解決這個問題。提前感謝。

+6

你試過什麼了?你必須向我們展示一些進展。 – sskoko

+1

並且還添加一些示例表格數據和預期結果 - 作爲格式化文本(而不是圖片)。請記住,如果有其他人完成了作業,您將不會學到太多東西。 – jarlh

+0

SELECT * FROM manufacturer where ManufacturerID =(請從產品 中選擇產品ID,其中productID =(從零售商處選擇產品ID,其中零售商ID = 1)) –

回答

0

根據你的問題的描述,我瞭解,這是您的要求:

select * 
from manufacturer 
where manufacturerID = (
    select manufacturerID 
    from Product 
    where productID = (
     select productID 
     from retailer 
     where [email protected]) 
    ) 

以上查詢是寫在子查詢格式,它可以很容易地轉換成聯接格式:

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