简体   繁体   English

在MySQL中获取不同计数的计数

[英]Getting a count of distinct counts in mySql

I have two Tables one for Teams one for Players What I am trying to find out is the total head count table, In other words I want to have a count of the total number of teams that have 2 members, the to all number of teams that have 3 members etc 我有两个表,一个是团队的,一个是玩家的,我想找出的是总人数表,换句话说,我想统计拥有2个成员的团队总数,即所有团队的总数有3个成员,等等

Here is the database structure. 这是数据库结构。

(Sidebar Question: I'm a newbee here: Is there a better way to post the SQL? ) (侧边栏问题:我在这里是个新手:是否有更好的方法发布SQL?)

CREATE  TABLE `formsfiles`.`Teams` (
  `ID` INT NOT NULL AUTO_INCREMENT ,
  `Name` VARCHAR(45) NULL ,
  PRIMARY KEY (`ID`) );


INSERT INTO `Teams` (`Name`) VALUES ('Sharks');
INSERT INTO `Teams` (`Name`) VALUES ('Jets');
INSERT INTO `Teams` (`Name`) VALUES ('Fish');
INSERT INTO `Teams` (`Name`) VALUES ('Dodgers');


CREATE  TABLE `Players` (
  `ID` INT NOT NULL AUTO_INCREMENT ,
  `Name` VARCHAR(45) NULL ,
  `Team_ID` INT NULL ,
  PRIMARY KEY (`ID`) );

INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Jim', '1');
INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Tom', '1');
INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Harry', '2');
INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Dave', '2');
INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Tim', '3');
INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Trey', '4');
INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Jay', '4');
INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Steve', '4');
INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Chris', '4');

What I want is a count Team sizes. 我想要的是团队人数。

I would like to see the following output 我想看下面的输出

Team_Size  Count
1          1
2          2
4          1

The simplest way would probably be: 最简单的方法可能是:

select team_count, count(*) from
(select count(*) team_count from players group by team_id) sq
group by team_count

(Although this won't include teams with no players in them.) (尽管其中不包括没有球员的球队。)

SQLFiddle here . SQLFiddle 在这里

First, you need the team sizes: 首先,您需要团队规模:

select t.id as teamId, count(p.id) as teamSize
from 
    `Teams` as t
    left join `Players` as p on t.id = p.teamId
group by
    t.id;

Notice that this will return the teams with zero players too. 请注意,这也将返回零个玩家的队伍。 If you don't want that, use inner join instead of left join . 如果您不想这样做,请使用inner join而不是left join

Now, use this query as a row source for your final query: 现在,将此查询用作最终查询的行源:

select teamSize, count(teamId)
from (
    select t.id as teamId, count(p.id) as teamSize
    from 
        `Teams` as t
        left join `Players` as p on t.id = p.teamId
    group by
        t.id) as a
group by teamSize;

Hope this helps 希望这可以帮助


Just one more thing. 还有一件事。

If you have big data sets, this query may hang. 如果您有大数据集,则此查询可能会挂起。 So it may be best to create a temp table, index it, and run the query on the temp table: 因此,最好创建一个临时表,对其进行索引,然后在临时表上运行查询:

drop table if exists temp_teamSize;
create temporary table temp_teamSize
    select t.id as teamId, count(p.id) as teamSize
    from 
        `Teams` as t
        left join `Players` as p on t.id = p.teamId
    group by
        t.id;
alter table temp_teamSize
    add unique index idx_teamId(teamId),
    add index idx_teamSize(teamSize);
select teamSize, count(teamId)
from temp_teamSize
group by teamSize;

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

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