简体   繁体   English

当每行的值发生变化时,计算有多少行具有指定值

[英]Count how many rows have a specified value when the value changes with each row

When I was querying a table by a specific id I would also count how many rows in that table shared the same name:当我通过特定 id 查询表时,我还会计算该表中有多少行共享相同的名称:

SELECT *, COUNT(name) FROM table WHERE id = 24601

Now I'm no longer querying a table by a specific id, instead getting all of the table's output.现在我不再通过特定的 id 查询表,而是获取表的所有输出。 Is there a way I could still get that count for each row?有没有办法我仍然可以获得每一行的计数?

SELECT *, COUNT(this specific row's name) FROM table

If you want to get the count of rows per name , then use GROUP BY :如果要获取每个name的行数,请使用GROUP BY

SELECT name, COUNT(*) AS count 
FROM table
GROUP BY name

You'll get one row per distinct value of name , with a second column that has the integer count.您将获得name每个不同值的一行,第二列具有整数计数。

If you also need the other columns for each row, you should write that as a separate query.如果您还需要每一行的其他列,则应将其编写为单独的查询。 Don't combine aggregated results with non-aggregated results in the same query.不要在同一查询中将聚合结果与非聚合结果合并。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM