Mysql 插入新行时,对表进行排序并相应更改列组
我的数据库中有下表:Mysql 插入新行时,对表进行排序并相应更改列组,mysql,Mysql,我的数据库中有下表: id userid totalpoints ranks 1 142 50 2 2 143 60 1 3 144 40 3 如果我想插入新行,则假定为否 4 145 55 现在,如何使用mysqli和php插入该行并根据总分相应地更改排名 为了获得以下输出: id userid totalpoints ranks 2 143 6
id userid totalpoints ranks
1 142 50 2
2 143 60 1
3 144 40 3
如果我想插入新行,则假定为否
4 145 55
现在,如何使用mysqli和php插入该行并根据总分相应地更改排名
为了获得以下输出:
id userid totalpoints ranks
2 143 60 1
4 145 55 2
1 142 50 3
3 144 40 4
您不应该在数据库中存储
等级
,因为这是派生值。相反,使用
SELECT ... ORDER BY totalpoints DESC
当你需要这些数据的时候。这会让你的分数从高到低依次排列
SELECT id,
userid,
totalpoints
row_number() OVER (ORDER BY totalpoints) ranks
FROM users
您可以使用此sql查询获取输出。确实是一种更好的方法。是的,您是对的。我把它弄得更复杂了,删除列可以解决我的问题。谢谢你的帮助。