转至:https://stackoverflow.com/questions/12113699/get-top-n-records-for-each-group-of-grouped-results 通过分组的排序及序号获取条数信息,可以使用到索引,没测试性能,不知道和mssql的cross apply性能差异性为多少,只是能实现相应的效果. #MySQL #please drop objects you've created at the end of the script #or check…
按name分组取最大的两个val: [比当前记录val大的条数]小于2条:即当前记录为为分组中的前两条 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name,a.val; val from tb where name=a.name order by val desc) order by a.name,a.val; ) order by a.name;…