是否可以用COUNT计算具有相同ID的所有行? [英] Is it possible to count all rows with the same id with COUNT?

查看:107
本文介绍了是否可以用COUNT计算具有相同ID的所有行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PostgreSQL 9.4

我有下表:

   id             player_id
serial PK          integer
---------------------------
   1                  1
   2                  3
  ...                ...
 123123               1

我需要计算 player_id = 1 的所有行。

I need to count all rows with player_id = 1. Is it possible to do with the COUNT aggregate?

现在可以按以下方式操作:

Now I do it as follows:

SUM(CASE WHEN player_id = 1 THEN 1 ELSE 0 END)


推荐答案

如果只需要计算 player_id 为1的行数,那么您可以执行以下操作:

If all you need is a count of the number of rows where player_id is 1, then you can do this:

SELECT count(*)
FROM your_table_name
WHERE player_id = 1;

如果要计算每个 <$ c的行数$ c> player_id ,那么您将需要使用 GROUP BY

If you want to count the number of rows for each player_id, then you will need to use a GROUP BY:

SELECT player_id, count(*)
FROM your_table_name
GROUP BY player_id;

这篇关于是否可以用COUNT计算具有相同ID的所有行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆