想知道sql语句是否正确

时间:2013-01-23 10:13:25

标签: mysql sql

附图是我在数据库表中的内容。我希望计算出对于独特的productSerial而言属于“IsRead”的数量。这里的productSerial不是主键,因此可以重复。但我不确定如何为此编写sql语句..

例如,结果应显示为:

10 - 3
11 - 2
12 - 1

我出来了,但我想知道这是否正确,并希望如果我错了,你们都可以纠正我。

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

enter image description here

6 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

不,这不正确。您还可以大大简化 WHERE 子句,如下所示:

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

答案 3 :(得分:0)

一种方式:

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

答案 4 :(得分:0)

怎么样:

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

答案 5 :(得分:0)

你可以轻松地像以下一样

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