MySQL8.0更新排序报错:Warning: #1287 Setting user variables within expressions is deprecated and ...

最近遇到一个更新排序的问题,记录分享给大家:

使用MySQL8.0.13版本执行如下排序查询时,报错Warning: #1287 Setting user variables within expressions is deprecated and will be removed in a future release. Please set variables in separate statements instead.

MySQL8.0更新排序报错:Warning: #1287 Setting user variables within expressions is deprecated and ..._第1张图片

像低版本时那样使用@rowNO查询

update game_fans_yes as t1 , ( SELECT (@rowNO := @rowNo+1) AS temp_rank, score, timeused, id FROM game_fans_yes ,(select @rowNO
:=0) as tempNO WHERE date_format(from_unixtime(regtime),'%Y-%m-%d') = date_format(now(),'%Y-%m-%d') order by score DESC, timeused
ASC ) as t2 SET t1.rank = t2.temp_rank where t1.id = t2.id

虽然,也能顺利执行,但是结果仅按表中数据原有的顺序进行排序,并没有达到目的。

于是上网查找原因,借鉴stackoverflow上的大佬的方法,实现了想要排序效果。

查询如下

update game_fans_yes as t1, ( SELECT id, score, timeused, row_number() over(order by score DESC, timeused ASC) as temp_rank FROM
game_fans_yes WHERE date_format(from_unixtime(regtime),'%Y-%m-%d') = date_format(now(),'%Y-%m-%d') order by score DESC, timeused
ASC ) as t2 SET t1.rank = t2.temp_rank where t1.id = t2.id

划重点:row_number() over()函数

参考链接:https://stackoverflow.com/questions/61282369/how-do-i-rewrite-this-query-without-setting-user-variable-inside-expressions-dep

你可能感兴趣的:(Mysql,mysql,数据库)