选择count where statement

时间:2014-10-24 13:56:04

标签: mysql sql

你好我在我的sql代码中计数count(返回= 0)时遇到了一些麻烦。

SELECT `return` 
FROM  `numbers`
WHERE list_id IN (
  SELECT id from listy where listy.number = "900000335300000939680320"
) 

以下是一个示例,如何使用此确切查询对return = 0进行计数。

http://sqlfiddle.com/#!2/49501f/2

5 个答案:

答案 0 :(得分:3)

当然,您需要在查询中使用COUNT(),并添加AND以仅查看return = 0的行。

SELECT COUNT(`return` ) AS returncount
FROM  `numbers`
WHERE list_id IN (
  SELECT id from listy where listy.number = "900000335300000939680320"
) AND
`return` = 0

答案 1 :(得分:2)

SELECT count(*) 
FROM  `numbers` 
WHERE list_id IN (SELECT id 
                  from listy 
                  where listy.number = "900000335300000939680320") 
                    AND `return` = 0;

答案 2 :(得分:1)

您还可以将sum与表达式一起使用,它将被评估为布尔值,您可以根据您的条件计算,也可以使用连接而不是子查询,下面的查询将为您提供{{ 1}}和return=0

return=1

DEMO

答案 3 :(得分:0)

AND之后使用WHERE子句缩小结果(最后一行),而不是计算你有多少SELECT COUNT()

SELECT COUNT(return)
FROM  numbers
WHERE list_id IN (
  SELECT id from listy where listy.number = "900000335300000939680320"
)
AND return = 0

答案 4 :(得分:0)

这可能会对您有所帮助:

SELECT `return`,count(*) FROM  `numbers` n join `listy` l
on n.list_id = l.id
where l.number = "900000335300000939680320" 
and n.return = 0
group by `return`

sql fiddle