表:
id column1 sorce
1 123 10
2 124 8
3 123 9
4 125 7
5 124 9
6 125 6
预期结果:
id column1 sorce
1 123 10
5 124 9
4 125 7
先进行分组,再根据分数降序排序.
上面的不对 select * from table_name order by column1,sorce desc group by column1;
这样子试试看看 可以不. select * from table_name order by column1,sorce desc limit 0,3;
SELECT * FROM test2 WHERE (NAME,score) IN(SELECT NAME ,MAX(score) FROM test2 GROUP BY NAME)
select * form table_name group by column1 having max(sorce)
select tmp.*
from
(select id,column1,sorce from test_name ORDER BY column1,sorce desc) tmp
group by tmp.column1;
更多关于group by,order by 的用法跟优化,请参见:
MySQL如何优化GROUP BY http://www.data.5helpyou.com/article237.html
MySQL如何优化ORDER BY http://www.data.5helpyou.com/article246.html
简单东西-group by与sum一起进行数据统计 ,参考这个:http://blog.csdn.net/wojiushiwo945you/article/details/51151390