SQL:比较来自不同表的两个计数

SQL:比较来自不同表的两个计数,第1张

SQL:比较来自不同表的两个计数

首先获取每个人的总和

# Sum of lakesSELECt pre, count(*) AS sum FROM lakes GROUP BY pre# Sum of mountainsSELECt pre, count(*) AS sum FROM mountains GROUP BY pre

然后将结果合并在一起,并选择一个国家的山之和小于湖泊数量的所有行:

SELECt l.pre AS pre, l.sum AS lake_count, m.sum AS mountain_countFROM(SELECt pre, count(*) AS sum FROM lakes GROUP BY pre) AS l JOIN(SELECt pre, count(*) AS sum FROM mountains GROUP BY pre) AS mON l.pre = m.preWHERe m.sum < l.sum


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5083799.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-16
下一篇 2022-11-16

发表评论

登录后才能评论

评论列表(0条)

保存