2013-01-23 70 views
0

附加的图像是我在我的数据库表中。我希望统计独特productSerial的“IsRead”的数量。 productSerial在这里不是主键,因此可以重复。但我不确定如何写这个sql语句..想知道如果sql语句正确

例如,结果应该会显示这样的:

10 - 3 
11 - 2 
12 - 1 

我想出了这一点,但我不知道是否是正确的,并希望你如果我错了,所有人都可以纠正我。

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

enter image description here

回答

7
select ProductSerial, count(*) 
from testing 
where IsRead = 'True' 
group by ProductSerial 
order by ProductSerial 
+0

对于第一次提姆e我看到一些没有太多代表的人仍然以5票赞成,1票赞成。 @VinnyRoe =) – bonCodigo

+0

感谢@bonCodigo。没有太多的代表,但我有20多年的时间,这不完全是一个难题! –

0
SELECT ProductSerial, Count(1) 
FROM testing 
WHERE IsRead = 'True' 
GROUP BY ProductSerial 
ORDER BY ProductSerial ASC 

应该给予同样的结果,是一个更简单的查询

0

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

SELECT DISTINCT productSerial, COUNT(IsRead) FROM testing 
WHERE IsRead = 'True' GROUP BY productSerial 
0

方式一:

Select productSerial , COUNT(IsRead) FROM testing where IsRead= 'True' group by productSerial 
0

如何:

select 
    ProductSerial, 
    Count(IsRead) as Count 
from 
    testing 
where 
    IsRead = 'True' 
group by 
    ProductSerial 
order by 
    ProductSerial ASC 
0

你可以轻松地像下面

SELECT `ProductSerial` , COUNT(`IsRead`) AS NumberOfProduct 
FROM `table_name` 
WHERE `IsRead` = 'True' 
GROUP BY `ProductSerial`