2

我正在使用 Northwind 数据库,但无法使以下查询正常工作 -

select *
from customers
    join orders
        on orders.customerID = customers.customerID
    join [Order Details]
        on orders.OrderID = [Order Details].orderID
    join Products (select Products.productID, Products.ProductName from Products)
        on [Order Details].productID = Products.productID
order by customers.customerID

我收到一条错误消息,指出第 7 行中的选择附近的语法不正确。

我想要做的是,在加入 Products 表时,它不会带来所有列,而只会带来 ProductName 和 ProductID。

有人可以解释我做错了什么吗?谢谢!

4

2 回答 2

2

您首先需要指定子查询,然后是别名。

select *
from customers
    join orders
        on orders.customerID = customers.customerID
    join [Order Details]
        on orders.OrderID = [Order Details].orderID
    join (select Products.productID, Products.ProductName from Products) Products 
        on [Order Details].productID = Products.productID
order by customers.customerID

改变

join Products (select Products.productID, Products.ProductName from Products) 

join (select Products.productID, Products.ProductName from Products) Products 
于 2013-09-15T07:42:46.197 回答
2

你可能想试试这个:-

select *
from customers
    join orders
        on orders.customerID = customers.customerID
    join [Order Details]
        on orders.OrderID = [Order Details].orderID
    join (select Products.productID, Products.ProductName from Products) Products
        on [Order Details].productID = Products.productID
order by customers.customerID
于 2013-09-15T07:43:35.573 回答