[英]SQL query involving specific counts
person(id primary key, name)
money(acct primary key, loaner)
loan(id primary key, acct)
How would I create an SQL query that shows for each loaner the names of persons who took more than four loans from that specific loaner? 我如何创建一个SQL查询,为每个借贷者显示从该特定借贷者那里借了四笔以上贷款的人的名字?
I tried count in the where clause but I am clueless so far. 我尝试在where子句中计数,但到目前为止我还是一无所知。
you could use the HAVING clause. 您可以使用HAVING子句。 or write a subquery to get all the counts, and use WHERE count > 4 in the outer query
或编写子查询以获取所有计数,并在外部查询中使用WHERE count> 4
SELECT p.id, p.name, m.loaner, COUNT(*) FROM person p
INNER JOIN loan l ON p.id = l.id
INNER JOIN money m ON l.acct = m.acct
GROUP BY id, name, lower
HAVING COUNT(*) > 4
What this does is create an aggregated record set with one record for each combination of id, name, and lender (loaner) along with a count of how many times that combination occurs. 这是创建一个聚合记录集,该记录集具有一个针对ID,名称和贷方(贷方)的每种组合的记录,并记录了该组合发生的次数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.