簡體   English   中英

SQL-帶有JOIN的SELECT語句

[英]SQL - SELECT statement with a JOIN

我正在使用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行中的select語法不正確。

我想做的是,在加入“產品”表時,它不會顯示所有列,而只會顯示ProductName和ProductID。

有人可以解釋我在做什么錯嗎? 謝謝!

您首先需要指定子查詢,然后再指定別名。

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 

您可能想嘗試一下:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM