MySQL显示2个Group BY的行数

MySQL显示2个Group BY的行数,mysql,sql,Mysql,Sql,事先抱歉,我不确定如何解释,但我正在尝试这样做: 我有一张像这样的桌子: id | make | model 1 | iphone | iphone 7 2 | iphone | iphone 8 3 | iphone | iphone 10 4 | samsung | galaxy note 5 5 | samsung | galaxy note 5 6 | samsung | galaxy note 4 7 | iphone | iphone 7 8 | iphone | iphone 7

事先抱歉,我不确定如何解释,但我正在尝试这样做:

我有一张像这样的桌子:

id | make | model

1 | iphone | iphone 7
2 | iphone | iphone 8
3 | iphone | iphone 10
4 | samsung | galaxy note 5
5 | samsung | galaxy note 5
6 | samsung | galaxy note 4
7 | iphone | iphone 7
8 | iphone | iphone 7
9 | htc | one
htc | one | 1 
iphone | iphone 7 | 3 
samsung | galaxy note 5 | 2 
SELECT a.* 
  FROM 
     ( SELECT make
            , model
            , COUNT(*) total 
         FROM my_table 
        GROUP 
           BY make
            , model
     ) a
  JOIN
     ( SELECT make
            , MAX(total) total 
         FROM 
            ( SELECT make
                   , model
                   , COUNT(*) total 
                FROM my_table 
               GROUP 
                  BY make
                 , model
          ) x 
       GROUP 
            BY make
     ) b
    ON b.make = a.make 
   AND b.total = a.total;
我希望结果如下所示:

id | make | model

1 | iphone | iphone 7
2 | iphone | iphone 8
3 | iphone | iphone 10
4 | samsung | galaxy note 5
5 | samsung | galaxy note 5
6 | samsung | galaxy note 4
7 | iphone | iphone 7
8 | iphone | iphone 7
9 | htc | one
htc | one | 1 
iphone | iphone 7 | 3 
samsung | galaxy note 5 | 2 
SELECT a.* 
  FROM 
     ( SELECT make
            , model
            , COUNT(*) total 
         FROM my_table 
        GROUP 
           BY make
            , model
     ) a
  JOIN
     ( SELECT make
            , MAX(total) total 
         FROM 
            ( SELECT make
                   , model
                   , COUNT(*) total 
                FROM my_table 
               GROUP 
                  BY make
                 , model
          ) x 
       GROUP 
            BY make
     ) b
    ON b.make = a.make 
   AND b.total = a.total;
因此,本质上,我只希望在对make和模型进行分组时,每个模型的行数最多


希望这对某些人来说是有意义的。提前谢谢你

因此,您希望显示
型号
,每个
品牌的最大计数

以下查询将仅执行此操作:

select make,model,count(*) as cnt
from phones p1
group by make,model
having cnt =  (select max(p2.cnt) 
               from 
                   (select make,model,count(*) as cnt
                          from phones
                          group by make,model
                   )p2
                where p2.make = p1.make                
               )
 ;


希望有帮助

你可能想要这样的东西:

id | make | model

1 | iphone | iphone 7
2 | iphone | iphone 8
3 | iphone | iphone 10
4 | samsung | galaxy note 5
5 | samsung | galaxy note 5
6 | samsung | galaxy note 4
7 | iphone | iphone 7
8 | iphone | iphone 7
9 | htc | one
htc | one | 1 
iphone | iphone 7 | 3 
samsung | galaxy note 5 | 2 
SELECT a.* 
  FROM 
     ( SELECT make
            , model
            , COUNT(*) total 
         FROM my_table 
        GROUP 
           BY make
            , model
     ) a
  JOIN
     ( SELECT make
            , MAX(total) total 
         FROM 
            ( SELECT make
                   , model
                   , COUNT(*) total 
                FROM my_table 
               GROUP 
                  BY make
                 , model
          ) x 
       GROUP 
            BY make
     ) b
    ON b.make = a.make 
   AND b.total = a.total;

使用mysql函数执行一些技巧,可以使用
substring\u index
group\u concat

select 
  `make`,
  substring_index(group_concat(`model` order by cnt desc), ',', 1) `model`,
  max(cnt) max_count
from (
  select `make`, `model`, count(1) cnt
  from yourtable
  group by `make`, `model`) t
group by `make`
看在小提琴里

一些解释:

  • 子查询
    t
    将查询每组
    make
    model
    的计数
  • group\u concat(cnt desc订购的型号)
    group by make
    将允许所有
    型号
    通过分隔符
    cnt desc订购
  • substring\u index
    是一个函数,它可以使用指定的分隔符拆分字符串,并根据我们传递的索引返回子字符串

所有这些功能您都可以查看mysql官方文档以了解更多信息。

这应该可以满足您的需要:@herrbischoff的可能重复项不,不是。只有两个iphone 7soh拍摄抱歉,我的错,有一个输入错误第一个记录应该是iphone 7。@草莓你能解释一下吗?@草莓是的,我知道了。谢谢你指出这一点。虽然我已经更新了我的答案。请看一看。是的,我想这没关系——尽管我不确定它是否能很好地伸缩。这一个成功了。你是个救命稻草谢谢你!我根本就没法得到这个,哈哈。