我有这些表格:
我如何创建一个SQL查询,为每个贷款人显示从该特定贷款人那里获得超过四笔贷款的人的姓名?我希望他借给他的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
通过此查询,您可以找到问题的第一部分 - 我应该添加什么?
答案 0 :(得分:0)
我会尝试一下,看看会发生什么:D
SELECT distinct *
FROM person as p_loaner_detailed
WHERE p_loaner_detailed.id in (
SELECT loanerId
FROM (
SELECT p.id, p.name, m.loaner as loanerId
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, loanerId
HAVING COUNT(*) > 4
)
)