首页 > 解决方案 > 无法理解连接表 SQL

问题描述

请参阅附表图片。

我的问题是:

找出2016年借钱最多的前5个职业

我的代码:

select c.occupation, count(*) no_mostborrow
from client c
Inner Join client c on c.clientID = b.clientID
where b.borrowDate >= '2016-01-01' and b.borrowDate < '2017-01-01'
group by c.clientoccupation, c.clientid
order by count(*) asc
limit 5

我觉得我在这里遗漏了一些东西,但我不确定是什么。我确定我完全离开了。非常感谢您的参与。

在此处输入图像描述

标签: mysqlsql

解决方案


要回答您的问题,您只需要occupationgroup by. 并且join需要正确:

select c.occupation, count(*) as no_mostborrow
from client c join
     borrower b
     on c.clientid = b.clientid
where b.borrowDate >= '2016-01-01' and b.borrowDate < '2017-01-01'
group by c.clientoccupation
order by count(*) asc
limit 5

推荐阅读