0

有两张桌子:银行和分行。

银行有:bank_name [red, blue, green,] bank_ID [1, 2 ,3]
分行:bank_ID [1, 1, 2, 3], branch_name [wright, james, will, phil]

我需要返回带有更多分行的银行名称,它是数字。

是否可以使用子查询来做到这一点?谢谢!


编辑答案:

选择 ban.bank_name, COUNT(bra.branch_name)

FROM 分行文胸 NATURAL JOIN 银行禁令

按银行名称分组

HAVING COUNT(bra.branch_name)=(SELECT MAX(COUNT(branch_name)) FROM branch GROUP BY bank_id);

我遇到了自然连接的问题,以及子查询如何影响它。

希望它可以帮助某人!

4

1 回答 1

0
SELECT b.bank_id, COUNT(*)
FROM 
    bank b
        LEFT JOIN branches br ON br.bank_ID = b.bank_ID
GROUP BY b.bank_id
ORDER BY COUNT(*) DESC
LIMIT 1

小提琴:http ://sqlfiddle.com/#!2/68af9/3

于 2013-02-16T19:09:16.947 回答