表架构:
customers (customerID: integer, fName: string, lName: string)
orders (orderID: integer, itemID: integer, aID: integer, customerID: integer, date: date)
items (itemID: integer, description: string, price: integer)
SQL:
SELECT customers.customerID, COUNT(orders.customerID)
FROM customers INNER JOIN orders
ON customers.customerID = orders.customerID
GROUP BY customers.customerID
我如何显示每位客户的订单数量结果?当我使用GROUP BY
时,它会移除COUNT(orders.customerID)
为NULL
的行。有没有办法显示0?
答案 0 :(得分:3)
不是过滤掉它的GROUP BY
。这是INNER JOIN
。
您正在寻找LEFT JOIN
:
select customers.customerID,
COUNT(orders.customerID)
from customers
left join orders on customers.customerID = orders.customerID
group by customers.customerID