微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

没有排序依据的MySQL最大计数

如何解决没有排序依据的MySQL最大计数

| 我有以下MysqL行:
SELECT age,count(*) AS total FROM pacient WHERE age BETWEEN 20 AND 40 GROUP BY age ORDER BY age
,我需要向其添加一列,仅显示每一行的count(*)的最大值。例如: 13 2 7 18 2 7 23 5 7 24 7 7 26 6 7 32 3 7 38 1 7 41 1 7 46 4 7 这将是3列,第3列显示7,因为7是进行count(*)的第二列中的最高数字。     

解决方法

        这里的解决方案:
select age,count(*),(select max(c) from 
        (select count(*) as c from pacient where age between 20 and 40 group by age) as x) as t 
from pacient 
where age between 20 and 40 
group by age 
order by age;
    ,        您是否尝试将查询与另一个查询一起包装?就像是
SELECT A.age,A.total,MAX(A.total) as max_value FROM (
    SELECT age,count(*) AS total 
    FROM pacient
    WHERE age BETWEEN 20 AND 40 
    GROUP BY age ORDER BY age) as A
GROUP BY A.age,A.total
    ,        
select 
      p.Age,count(*)  CountPerAge,max(ar.AllRecs) AllRecs
   from
      pacient p,( select count(*) AllRecs 
           from pacient p2
           where p2.age between 20 and 40 ) ar
   where 
      p.age between 20 and 40
   group by
      p.age
通过在没有联接条件的情况下联接第二个\“ subselect \”,它将得到笛卡尔结果...由于它是一个没有group by的计数,因此它将始终返回单个记录,因此将联接所有年龄段否则行。该值的MAX()没问题,因为它是唯一的记录,将按原样返回。     ,        使用SQL VIEWS而不是使用子查询总是好事。因为VIEW将具有已编译的结果。
CREATE VIEW subqueryView 
    SELECT age,count(*) AS total 
    FROM pacient
    WHERE age BETWEEN 20 AND 40 
    GROUP BY age ORDER BY age

SELECT A.age,MAX(A.total) as max_value FROM (SELECT FROM subqueryView) as A
GROUP BY A.age,A.total
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。