SQL Server:" Jamba Juice已经下了多少订单"?多对多的关系

时间:2016-11-21 03:46:32

标签: sql sql-server many-to-many

我现在一直试图写这个查询2个小时。请帮忙。

问题和#34; Jamba Juice已经下了多少订单"?

这就是我所拥有的。

SELECT 
    Customers.customerNumber, Customers.customerName, 
    COUNT(orderNumber) AS orders_placed
FROM 
    Customers
WHERE 
    Customers.customerName = 'Jamba Juice'
JOIN 
    Orders ON Customers.customerNumber = Orders.customerNumber
GROUP BY 
    Customers.customerNumber, Customers.customerName;

我得到的错误信息是

  

Msg 156,Level 15,State 1,Line 29
  关键字' JOIN'

附近的语法不正确

3 个答案:

答案 0 :(得分:1)

Join应该在WHERE之前。

SELECT Customers.customerNumber, Customers.customerName, COUNT(orderNumber) AS orders_placed
FROM Customers
JOIN Orders ON Customers.customerNumber= Orders.customerNumber
WHERE Customers.customerName= 'Jamba Juice'
GROUP BY Customers.customerNumber, Customers.customerName;

答案 1 :(得分:0)

您的JOIN声明应该在FROM Customers之后和where子句

之前
SELECT Customers.customerNumber, Customers.customerName, COUNT(orderNumber) AS orders_placed

FROM Customers

JOIN Orders ON Customers.customerNumber= Orders.customerNumber

WHERE Customers.customerName= 'Jamba Juice'

GROUP BY Customers.customerNumber, Customers.customerName;

答案 2 :(得分:0)

似乎语法错误。请注意表格需要首先加入,然后加入条件和过滤条件,如下所示。

SELECT Customers.customerNumber, Customers.customerName, COUNT(orderNumber) AS orders_placed
FROM Customers
JOIN Orders ON Customers.customerNumber= Orders.customerNumber
WHERE Customers.customerName= 'Jamba Juice'
GROUP BY Customers.customerNumber, Customers.customerName;