0

所附图像是我的数据库表中的内容。我想计算一个唯一 productSerial 的“IsRead”数量。这里的productSerial 不是主键,所以可以重复。但是我不确定如何为此编写sql语句..

例如,结果应显示为:

10 - 3
11 - 2
12 - 1

我提出了这个,但我想知道它是否正确,如果我错了,希望大家能纠正我。

Select DISTINCT productSerial
  FROM (SELECT COUNT(IsRead) where IsRead = 'True' FROM testing);  

在此处输入图像描述

4

6 回答 6

7
select ProductSerial, count(*)
from testing
where IsRead = 'True'
group by ProductSerial
order by ProductSerial
于 2013-01-23T10:16:15.977 回答
0
SELECT ProductSerial, Count(1)
FROM testing
WHERE IsRead = 'True'
GROUP BY ProductSerial
ORDER BY ProductSerial ASC

应该给出相同的结果,并且是一个更简单的查询

于 2013-01-23T10:17:26.817 回答
0

不,这是不正确的。您还可以像这样大大简化WHERE子句:

    SELECT DISTINCT productSerial, COUNT(IsRead) FROM testing 
WHERE IsRead = 'True' GROUP BY productSerial
于 2013-01-23T10:18:18.180 回答
0

单程:

Select productSerial , COUNT(IsRead) FROM testing where IsRead= 'True' group by productSerial 
于 2013-01-23T10:22:32.480 回答
0

怎么样:

select
    ProductSerial,
    Count(IsRead) as Count
from
    testing
where
    IsRead = 'True'
group by
    ProductSerial
order by
    ProductSerial ASC
于 2013-01-23T10:23:07.900 回答
0

你可以像下面一样轻松地做

SELECT `ProductSerial` , COUNT( `IsRead` ) AS NumberOfProduct
FROM `table_name`
WHERE `IsRead` = 'True'
GROUP BY `ProductSerial` 
于 2013-01-23T10:57:39.047 回答