我有2个表,例如Customers和Purchases表。而且我试图让那些在某一年内购买超过1件的顾客。所以这是我当前的查询
select c.name, c.email
from customers c inner join purchases p
on c.customerid = p.customerid
where p.year = 2002
group by c.name, c.email
我现在遇到的问题是这个查询会导致任何购买的人不是我想要的。所以我想编辑为:
select c.name, c.email
from customers c inner join purchases p
on c.customerid = p.customerid
where p.year = 2002 and p in
(select p.customerid, count(p.*) as total
from purchases
where total > 1
group by p.customerid)
group by c.name, c.email
此查询已完整语法错误。有人帮我问一个想法。
答案 0 :(得分:1)
可能是这样的
SELECT C.Name, C.Email
FROM Customers C INNER JOIN Purchases P ON C.CustomerID = P.CustomerID
WHERE P.Year = 2002
GROUP BY C.Name, C.Email
HAVING COUNT(P.CustomerID) > 1
答案 1 :(得分:0)
SELECT *
FROM Customers
WHERE (SELECT COUNT(*)
FROM Purchases
WHERE Year = 2002
AND CustomerID = Customers.CustomerID) > 1