mysql 自我总结

group by  和  DISTINCT

          group by按组划分,每组只显示1条记录

          distinct后接多个字段,保证每个字段组合的记录唯一(distinct id,name from )

groupby having 和 where 

         SELECT *,(SUM(win)/COUNT(*)) as present  FROM `xxtable`  GROUP BY hero HAVING present>0.1

SELECT *,(SUM(win)/COUNT(*)) as present  FROM `xxtable` where present>0.1 GROUP BY hero (语句错误,找不到present)

where 子句的作用是在对查询结果进行分组前,将不符合where条件的行去掉,即在分组之前过滤数据,条件中不能包含聚组函数,使用where条件显示特定的行

having用在聚合函数中,判断聚合函数(sum,count,avg,)的值是否大于某个值




实例特训:

1.英雄联盟多玩魔盒

mysql 自我总结_第1张图片

A:获取胜率排名前2的英雄和对局

SELECT *,(SUM(win)/COUNT(*)) as present  FROM `aa` WHERE userid=1 GROUP BY hero ORDER BY present desc limit 2

B:获取胜率大于0.5的英雄和对局

SELECT *,(SUM(win)/COUNT(*)) as present  FROM `aa` WHERE userid=1 GROUP BY hero HAVING present>0.5 ORDER BY present desc

你可能感兴趣的:(mysql 自我总结)