了解连接表SQL的麻烦

问题描述 投票:0回答:1

请参阅所附的表格图像。

我有问题:

查找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

我觉得我在这里想念什么,但是我不确定。我确定我已经完全离开了。非常感谢您的宝贵时间。

enter image description here

mysql sql
1个回答
1
投票

要回答您的问题,只需要occupation中的group 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
© www.soinside.com 2019 - 2024. All rights reserved.