mysql group by/having/distinct 查询使用例子
2015-07-21 21:56
876 查看
表的内容如下:
//查询单分数最高的人ID
select stuid,max(score) from stu_select_class;
// 查询各科总分2-3名的学生
select stuid, sum(score) from stu_select_class group by stuid limit 1,2;
//查询每科的最高分学生
select classname,stuid,max(score) from stu_select_class group by classname;
//查询总分低于140的学生
select stuid, sum(score) from stu_select_class group by stuid having sum(score)<140;
//查询总平均分在60-80的学生
select stuid, avg(score) from stu_select_class group by stuid having avg(score) between 60 and 80;
//查询。。。。的学生
select stuid, sum(score),avg(score) from stu_select_class group by stuid having sum(score)<140 and avg(score) <80;
//查询总分。。的学生人数。
select distinct count( stuid) from stu_select_class group by stuid having sum(score)<140 and avg(score) <80;
//查询。。。的学生人数
select count(distinct stuid) from stu_select_class where classname='english' and score <100;
//查询单分数最高的人ID
select stuid,max(score) from stu_select_class;
// 查询各科总分2-3名的学生
select stuid, sum(score) from stu_select_class group by stuid limit 1,2;
//查询每科的最高分学生
select classname,stuid,max(score) from stu_select_class group by classname;
//查询总分低于140的学生
select stuid, sum(score) from stu_select_class group by stuid having sum(score)<140;
//查询总平均分在60-80的学生
select stuid, avg(score) from stu_select_class group by stuid having avg(score) between 60 and 80;
//查询。。。。的学生
select stuid, sum(score),avg(score) from stu_select_class group by stuid having sum(score)<140 and avg(score) <80;
//查询总分。。的学生人数。
select distinct count( stuid) from stu_select_class group by stuid having sum(score)<140 and avg(score) <80;
//查询。。。的学生人数
select count(distinct stuid) from stu_select_class where classname='english' and score <100;
相关文章推荐
- MySQL 用户权限详细汇总
- MySQL中删除所有表的方法
- Mac安装MySQLdb
- 服务器保持与Mysql的连接
- mysql乱码的好文
- Ubuntu下mysql设置远程访问
- Node.js Express连接mysql完整的登陆注册系统(windows)
- Ubuntu14安装mysql命令
- Mysql数据库笔记
- Ubuntu mysql 中文,启动,重启,远程连接相关问题
- mysql的having语句
- mysql分表的3种方法
- 关于mysql主从复制error_code 1593的小异常BUG
- MySQL INTO OUTFILE导出导入数据|mysqldump
- MySQL INTO OUTFILE导出导入数据|mysqldump
- mysql in 子查询 效率慢 优化(转)
- mysql source 乱码
- MYSQL 修改密码
- 解决mySQL占用内存超大问题
- mysql查询锁表及解锁