这是DB Schema:
Books (bookid, title, author, year)
Customers (customerid, name, email)
Purchases (customerid, bookid, year)
Reviews (customerid, bookid, rating)
Pricing (bookid, format, price)
如何找到在2003年购买多本图书的客户(显示他们的姓名和电子邮件地址)?
谢谢!
答案 0 :(得分:5)
SELECT name, email, COUNT(p.customerId) as purchases_in_2003
FROM Customers c
INNER JOIN Purchases p ON c.customerId = p.customerId
WHERE p.year = 2003
GROUP BY name, email
HAVING purchases_in_2003 > 1
答案 1 :(得分:2)
几乎就像你的英语问题所说的那样...只是翻译成SQL ......
Select * From Customers C
Where (Select Count(*) From Purchases
Where customerid = C.customerid
And Year = 2003) > 1
答案 2 :(得分:0)
还有一个选择:
select
cu.name
,cu.email
,count(*) as books_purchased
from
customers cu
,purchases pu
where cu.customerid = pu.customerid
and pu.year = 2003
group by
cu.name
,cu.email
having
count(*) > 1