基于一个元素的查询

时间:2016-01-21 08:34:04

标签: mysql sql

我获得了以下数据库架构

Cows (cowID, cowName, cowAge)
CowPurchaser (purchaserID, name, address)
PurchaseRecord (purchaserID, cowID, year)

每年,奶牛购买者“FARMLAND”购买至少一头牛,找到购买的奶牛数量。输出是元组的集合,表示由“FARMLAND”购买的年份和奶牛数量

我不知道如何处理这个问题。我试过了

SELECT (pr.year, COUNT(c.name))
FROM PurchaseRecord pr, CowPurchaser cp, Cows c
WHERE cp.name = "FARMLAND" AND COUNT(c.name) >= 1

这不是我想要的结果。我能做错什么?我该如何解决?

1 个答案:

答案 0 :(得分:0)

SELECT pr.year, COUNT(c.name)
FROM PurchaseRecord pr
     INNER JOIN CowPurchaser cp ON pr.purchaserID=cp.purchaserID
     INNER JOIN Cows c ON pr.cowID=c.cowID
WHERE cp.name = "FARMLAND" 
GROUP BY pr.year
HAVING COUNT(c.name) >= 1