如何将ROW INDEX作为列添加到SQL SELECT查询中? [英] How to add ROW INDEX as a column to SQL SELECT query?

查看:219
本文介绍了如何将ROW INDEX作为列添加到SQL SELECT查询中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这样的SQL查询:

Assume I've SQL query like this:

SELECT id, name, index(not a real column) FROM users ORDER BY rating DESC

我想将列添加到表示记录索引的选定列中.

I want to add column to selected columns that will represent the index of the record.

示例:

 id    name  rating
 1     a     4
 2     b     2
 3     c     8
 4     d     5

对于此表,我想获取:

 id    name  rating  index
 3     c     8       1
 4     d     5       2
 1     a     4       3
 2     b     2       4

推荐答案

尝试以下操作以获取row_index:

Try the following to get the row_index:

set @row_num = 0; 
SELECT id,name,rating, @row_num := @row_num + 1 as row_index FROM users
ORDER BY rating desc;

这篇关于如何将ROW INDEX作为列添加到SQL SELECT查询中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆