引用一个论坛的帖子学习 group ib order max组合查询
2009-10-19 10:19
330 查看
保存用户考试信息的表 score
user_id | test_time | test_score
-----------------
1 | 2009.1.1 | 50
1 | 2009.1.2 | 60
2 | 2009.1.2 | 60
1 | 2009.1.5 | 80
2 | 2009.1.6 | 90
2 | 2009.1.8 | 70
group by
SELECT * FROM score GROUP BY user_id ORDER BY test_score DESC
但MYSQL是先GROUP后ORDER的,行不通。
max来搞定
SELECT *, MAX(test_score) s FROM score GROUP BY user_id ORDER BY s DESC
但这样test_time 就不对应了。
先ORDER后GROUP
SELECT tmp.* FROM (SELECT * FROM score ORDER BY test_score DESC) tmp GROUP BY tmp.user_id
user_id | test_time | test_score
-----------------
1 | 2009.1.1 | 50
1 | 2009.1.2 | 60
2 | 2009.1.2 | 60
1 | 2009.1.5 | 80
2 | 2009.1.6 | 90
2 | 2009.1.8 | 70
group by
SELECT * FROM score GROUP BY user_id ORDER BY test_score DESC
但MYSQL是先GROUP后ORDER的,行不通。
max来搞定
SELECT *, MAX(test_score) s FROM score GROUP BY user_id ORDER BY s DESC
但这样test_time 就不对应了。
先ORDER后GROUP
SELECT tmp.* FROM (SELECT * FROM score ORDER BY test_score DESC) tmp GROUP BY tmp.user_id
相关文章推荐
- MySql学习(二) —— where / having / group by / order by / limit 简单查询
- Effective c++学习笔记——条款10:令operator=返回一个*this的引用
- 在SQL Server中使用SQL语句查询一个存储过程被其它所有的存储过程引用的存储过程名
- 又一个学习论坛
- 查询语句中select from where group by having order by的执行顺序
- hibernate的学习心得(引用知乎的一个author)
- ###Jsp+Servlet购物商城day04.2:订单模块难点总结。【购物商城最大的难点:【封装一个Order对象】查询封装三张表】
- oracle 基础SQL语句 多表查询 子查询 分页查询 合并查询 分组查询 group by having order by
- 在SQL Server中使用SQL语句查询一个存储过程被其它所有的存储过程引用的存储过程名
- mysql查询报错: ORDER BY clause is not in GROUP BY..this is incompatible with sql_mode=only_full_group_by
- mysql 查询多条结果,合并成一个结果,使用 group_concat()函数
- 5、步步为营VS 2008 + .NET 3.5(5) - LINQ查询操作符之Select、Where、OrderBy、OrderByDescending、GroupBy、Join、GroupJoin及其对应的查询语法
- MongoDB中的一个分组统计的查询(Group ... Distinct)
- 学习命令模式的一个帖子
- 查询语句中select from where group by having order by的执行顺序
- 一步一步跟我学习lucene(12)---lucene搜索之分组处理group查询
- ofbiz学习——创建一个查询页面
- LeetCode 笔记系列六 Reverse Nodes in k-Group [学习如何逆转一个单链表]
- LINQ 标准的查询操作符 分组 group by into 、select new 、orderby descending、from in
- Effective c++学习笔记——条款10:令operator=返回一个*this的引用