SQL选择COUNT(值=值)可能? [英] SQL Select COUNT(Value=Value) Possible?

查看:214
本文介绍了SQL选择COUNT(值=值)可能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算所有只有我想要的值的行:

I want to Count all the rows that only have the value I want like this:

SELECT Users.Balance,
       Users.FreeBids,
       COUNT(Bids.Burned = 0) AS 'ActiveBids',
       COUNT(Bids.Burned = 1) AS 'BurnedBids'
FROM   Users
       INNER JOIN Bids
         ON Users.ID = Bids.BidderID
WHERE  Users.ID = 2
GROUP  BY Users.Balance,
          Users.FreeBids  

它说无效的语法Neat'=' '。
如何计算Burned = 1和Burned = 0的行?

It says "Invalid Syntax Neat '=' It works perfectly without the '='. How can I count the rows that Burned=1 in them and Burned=0 in them?

谢谢,
Dan

Thanks, Dan

推荐答案

使用 CASE 语句

COUNT(CASE WHEN Bids.Burned=0 THEN 1 END) AS 'ActiveBids', 
COUNT(CASE WHEN Bids.Burned=1 THEN 1 END) AS 'BurnedBids'

有一个隐式 ELSE NULL COUNT 只计算 NOT NULL 值,因此这将给您所需的结果。

There is an implicit ELSE NULL. COUNT only counts NOT NULL values so this will give you the result you need.

这篇关于SQL选择COUNT(值=值)可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆