假设我有这样的SQL查询:
SELECT id, name, index(not a real column) FROM users ORDER BY rating DESC
我想将列添加到将代表记录索引的选定列中。
例子:
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:
set @row_num = 0; SELECT id,name,rating, @row_num := @row_num + 1 as row_index FROM users ORDER BY rating desc;