两个SQL COUNT()查询?

两个SQL COUNT()查询?,第1张

两个SQL COUNT()查询

一种方法是将表与自身连接:

select   count(*) as TotalCount,   count(s.id) as QualifiedCountfrom   MyTable aleft join   MyTable s on s.id = a.id and {some conditions}

另一种方法是使用子查询:

select   (select count(*) from Mytable) as TotalCount,   (select count(*) from Mytable where {some conditions}) as QualifiedCount

或者,您可以将条件放在一个案例中:

select   count(*) as TotalCount,   sum(case when {some conditions} then 1 else 0 end) as QualifiedCountfrom   MyTable


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存