0

添付の画像は、DB テーブルにあるものです。一意の 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 に答える