mysql分组多个数据计算

当需要我们做分组并计算每组中的数据时,可以把查询的结果作为一个表,然后再进行运算。

表a

id cname class
1000 a 1
1001 b 2
1002 c 3
表b

id sname age cid
2000 小红 12 1000
2001 小白 11 1001
2002 小黑 10 1002
表c

id cid late
3000 1000 2
3001 1001 1
3002 1002 3

/*查询每个年级学生的总数*/

select cid,COUNT(id) from b group by cid;


/*查询每个年级迟到情况*/

select b.cid, c.late from c,b where c.id=b.id;


/*统计每年级出勤率*/

select b1.bcid,CONCAT(ROUND(b1.zong/c1.la*100,1),'%') from 
(select cid as bcid,COUNT(id) as zong from b group by cid) b1,
(select b.cid as ccid, c.late as la from c,b where c.id=b.id;) c1 
where b1.bcid=c1.ccid GROUP BY b1.bcid;


扫描二维码关注公众号,回复: 1236947 查看本文章


数据表随便弄得,查询的结果可能有误,大家知道个意思就好了。








猜你喜欢

转载自blog.csdn.net/qq594913801/article/details/78500366