当前位置:数据库 > MySQL >>

mysql同时用到group by和非group by的聚集函数怎么办

比如select count(s) from table group by id 那么查询到是根据id分组的count信息,如果我需要求每个组count值除以table中所有count值(即每个组所占比例)怎么办
追问:那如果我的table是一个子查询得到的结果,那是不是要写两遍子查询?这样效率不是很低吗?group by之后count不是算出每个组中的计数嘛,然后我需要知道每个组的计数占总数的比例,怎么写?
答案:select count(s),count(s)/(select count(s) from table ) from table group by id
其他:听不明白,你补充一下,洒家来帮你。 那你再求出 表中所有的行数就是了。
select a.id,a.cnt/b.total
from 
   (select id,count(s) cnt from table group by id)a,
      (select count(1) total from table)b

上一个:mysql外键问题。跪求易做图指教。。 Can't create table 'test.homework' (errno: 150)问题
下一个:mysql查询问题

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,