SQL SELECT与几个嵌套的JOIN

时间:2014-09-09 13:51:25

标签: sql sql-server join

我有这种关系,我需要编写一个查询,给定@SellerId将返回CustomerFirstNameCustomerLastNameCustomerSSNSellerComissionProductNameProductDescription

我迷失了JOIN语法,逻辑是找到 SellerId = @SellerId 的所有客户记录 从UserId = CustomerId开始FirstName,LastNameSSNUsersCustomerIdSalesProducts所有字段加入ProductId然后加入与Seller上的所有字段(SoldToFName,SoldToLastName,SoldToSSN,ProductName,ProductDescription,SellerComission)表一起加入。换句话说,我需要一张特定SELECT UserId, FirstName , LastName , SSN FROM Users JOIN Customers ON (Users.UserId = Customers.CustomerId) WHERE Customers.SellerId = @SellerId 所有销售的表格,其中每条记录都会显示如下:

{{1}}

enter image description here

{{1}}

这将返回连接到卖家的所有客户。我不知道如何正确嵌套JOIN

1 个答案:

答案 0 :(得分:0)

感谢Sean Lange关于语法的提示

SELECT UserId, FirstName , LastName , SSN , Products.ProductId , Products.Name , Products.Description , Sales.SellerComission FROM Users 
    JOIN Customers ON (Users.UserId = Customers.CustomerId) 
    JOIN Sales ON (Customers.CustomerId=Sales.CustomerId)
    JOIN Products ON (Products.ProductId=Sales.ProductId)

    WHERE Customers.SellerId = @SellerId