mysql 排序并增加序号,分数相同并列

1,排序,根据总分 增加序号

表结构:

sql:

SET @row=0;
SELECT b.*, @row:[email protected]+1 rownum FROM (
select *,SUM(a.achievement) as achievementAll from cp_cj as a
where 1=1 GROUP BY a.studentNum order by SUM(a.achievement) DESC limit 0,30
) as b

结果:

2,排序,根据总分 增加序号,并且按照总分相同的并列

sql:

select c.* ,ifnull((
select count(*) from
(
select *,SUM(a.achievement) as achievementAll from cp_cj as a
where 1=1 GROUP BY a.studentNum order by SUM(a.achievement) DESC 
) as b
where c.achievementAll < b.achievementAll

),0)+1 as rownum from
(
select *,SUM(a.achievement) as achievementAll from cp_cj as a
where 1=1 GROUP BY a.studentNum order by SUM(a.achievement) DESC limit 0,30
) as c

结果:

原文地址:https://www.cnblogs.com/janeaiai/p/9039843.html

时间: 2024-08-30 12:47:52

mysql 排序并增加序号,分数相同并列的相关文章

mysql 排序

一.主要内容简介 MySQL排序是个老生长谈的话题,这次我们想由浅入深详细说说MySQL的几种排序模式,怎么选择不同排序模式,以及如何优化排序. 同时也希望通过本文能解决大家的几个疑问: MySQL什么时候做排序,怎么判断需要进行排序: MySQL有几种排序模式,有什么方法让MySQL选择不同的排序模式: MySQL排序跟 read_rnd_buffer_size 有啥关系,在哪些情况下增加 read_rnd_buffer_size 能优化排序效率: 怎么判断MySQL使用了磁盘排序,怎么避免或

C# WinForm DataGridView 给标题列增加序号及格式化某个字段

DataGridView 给标题列增加序号 private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) { DataGridViewTextBoxColumn dgv_Text = new DataGridViewTextBoxColumn(); for (int i = 0; i < dataGridView1.Rows.Count; i++) { i

SQL排序后将序号填入指定字段

1.显示行号 如果数据没有删除的情况下主键与行号是一致的,但在删除某些数据,行号就与主键不一致了,这时需要查询行号就需要用新的方法,在SQL Server2005之前,需要使用临时表,但在SQL Server2005中,使用ROW_NUMBER()非常方便. 以下是一个查询语句,意思是按照cid将数据排序,然后将排序后的序号填入新建的字段rowNum. select row_number() over (order by cid) as rowNum,* from t_gene 查询结果: 2.

mysql记录集中记录序号,MySQL中实现rownum功能类似的语句(转)

MySQL 如何实现 Oracle 的 ROWNUM mysql> SELECT * FROM frutas;+-----------+----------+| nombre    | color    |+-----------+----------+| fresa     | rojo     || platano   | amarillo || manzana   | verde    || uva       | verde    || pera      | verde    || m

MySQL排序:SELECT ORDER BY

SELECT 和ORDER BY结合进行排序: products表如下: a 按产品名称列进行排序: b 按多个列进行排序,默认排序顺序为升序: c 指定排序方向,即指定为降序: d 对多个列进行排序,先按产品价格降序排列,再按产品名称排列 e 找出某一列最高值或者最低值 MySQL排序:SELECT ORDER BY,布布扣,bubuko.com

Datatables使用之增加序号(1.10新版实现)

以前用EasyUI的,但是最近想做前端的表格,找到了JQuery的Datatables插件,非常不错. 本来以为给表格加一个序号很简单,可是看了Datatables的官方实现看似代码也不简单,测试发现了一些问题,用的不爽.网络上并没有搜索到好的解决方案,就跟踪代码,自己找到了一种方法,本文在最后就给出了这种实现. 1.版本问题 最新的版本1.10,和以前版本做了很大的调整,但是也部分兼容老版本Api的使用.不过官网给出的例子都是新版的使用方式. 新老版本对照表,可以参照 http://dt.th

(转)Repeater中增加序号自增列

<%# Convert.ToString(Container.ItemIndex+1)%> 当Repeater空为时,提示没有数据... <FooterTemplate> <tr style="color:Red; font-size:18px;" runat="server" Visible="<%#rpt.Items.Count==0 %>" > <td colspan="8&q

cxGrid 增加序号 (非数据库绑定模式) (测试通过)

cxGrid 增加序号 (非数据库绑定模式) ----------------------------------- 1. 选在 adoQuery 控件 , 鼠标右键菜单中 选择 Fields Editor 2. 在  adoQuery 控件 Fields Editor 中 鼠标右键菜单  (1)  Add all fields (2)  New field  --->  Name ==>     curRowNo                         Type    ==>

sql语句-排序后加入序号再运算判断取想要的项

select a.id as aid,b.id as bid,a.city,a.cang,a.sid,a.time as atime,b.time as btime,a.price as aprice,b.price as bprice,a.pm as apm,b.pm as bpm from (select id,city,cang,sid,time,price,@rank:[email protected]+1 as pm  from cai,(SELECT @rank:=0) B  gro