[英]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.