[英]How to group duplicated rows and count them?
my table:我的表:
id | item_id
1 | 5
2 | 5
3 | 7
4 | 2
sql: sql:
$countWeek = $conn->query("SELECT count(item_id) FROM `myTable` GROUP BY `item_id`")->fetchColumn();
As you can see i have 2 duplicated rows with item_id = 5
i want to group these duplicated rows and output 3
rows on the count, but when i do echo $countWeek
it output 1
, why?如您所见,我有 2 个重复的行,
item_id = 5
我想将这些重复的行分组并在计数上输出3
行,但是当我执行echo $countWeek
它输出1
,为什么?
When i change the above sql to:当我将上面的 sql 更改为:
$countWeek = $conn->query("SELECT item_id FROM `myTable` GROUP BY `item_id`")->rowCount();
It returns the correct value, but i don't want to use rowCount()
because i only need to count the rows and fetchColumn()
with count()
is far better in terms of speed.它返回正确的值,但我不想使用
rowCount()
因为我只需要计算行数,而fetchColumn()
和count()
在速度方面要好得多。
You could use counct(distinct item_id)您可以使用 counct(distinct item_id)
SELECT count(distinct item_id)
FROM `myTable`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.