SQL查询返回条目,其他表中没有出现

时间:2022-08-31 01:57:26

I have two tables, orders and customers, and I am trying to return the customerID and name of customers with no orders.

我有两个表,订单和客户,我试图返回没有订单的客户ID和客户名称。

customers
customerID: integer
name: string

orders
orderID: integer
itemID: integer
customerID: integer
date: date

What I currently have is not returning any results:

我目前所拥有的不会返回任何结果:

SELECT customers.customerID, customers.fName, orders.date
FROM orders INNER JOIN customers
ON orders.customerID = customers.customerID
GROUP BY orders.customerID
HAVING COUNT(*) = 0

1 个解决方案

#1


1  

You need a LEFT OUTER JOIN to accomplish this:

你需要一个LEFT OUTER JOIN来完成这个:

SELECT customers.customerID, customers.fName
FROM customers LEFT OUTER JOIN orders on customers.customerID = orders.customerID
WHERE orders.customerID IS NULL

#1


1  

You need a LEFT OUTER JOIN to accomplish this:

你需要一个LEFT OUTER JOIN来完成这个:

SELECT customers.customerID, customers.fName
FROM customers LEFT OUTER JOIN orders on customers.customerID = orders.customerID
WHERE orders.customerID IS NULL