繁体   English   中英

如何计算表中某列中具有相同值的行数?

[英]How do you count the number of rows in a table that have the same value in value in a column?

假设我有以下简单表格:

create table mytable (
    desid bigint not null,
    ancid bigint not null
  );

insert into mytable (ancid,desid) values (1,10);
insert into mytable (ancid,desid) values (1,20);
insert into mytable (ancid,desid) values (1,21);
insert into mytable (ancid,desid) values (1,22);
insert into mytable (ancid,desid) values (2,30);
insert into mytable (ancid,desid) values (3,40);
insert into mytable (ancid,desid) values (3,41);
insert into mytable (ancid,desid) values (3,42);
insert into mytable (ancid,desid) values (3,43);

什么是 SQL 命令将打印出以下信息:

4 rows with ancid=1
1 rows with ancid=2
4 rows with ancid=3

您可以通过按ancid分组来获取信息:

SELECT   ancid, COUNT(*)
FROM     mytable
GROUP BY ancid

暂无
暂无

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

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