问题
Suppose I have the following simple table:
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);
What is the SQL command that will print out the following information:
4 rows with ancid=1
1 rows with ancid=2
4 rows with ancid=3
回答1:
You can get the information by grouping by the ancid
:
SELECT ancid, COUNT(*)
FROM mytable
GROUP BY ancid
来源:https://stackoverflow.com/questions/65782256/how-do-you-count-the-number-of-rows-in-a-table-that-have-the-same-value-in-value