mysql - 无法理解连接表 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
我觉得我在这里遗漏了一些东西,但我不确定是什么。我确定我完全离开了。非常感谢您的参与。
解决方案
要回答您的问题,您只需要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
推荐阅读
- c++ - 无法使用 fstream 写入文件 - 我似乎不明白为什么
- python - Wordcloud 使用短语而不是单个单词
- reactjs - signInWithEmailAndPassword 失败:第二个参数“密码”必须是一个有效的字符串与反应
- google-cloud-platform - 管理员或 GCP 中的用户登录时出现 Windows 2012 R2 RDS 黑屏
- reactjs - react-router中的Link和`window.history.pushState()`有什么区别?
- jquery - 如何在 Asp.net core mvc 视图中检查带有枚举值的单选按钮
- rabbitmq - RabbitMQ 在节点崩溃后删除损坏的队列
- python - 如何在python中继承类复杂?
- openlayers - OpenLayers 捕捉指针移动到特征
- ssl - 有效的 SSL 证书,本地主机