User-defined MySQL variables in Laravel 3?

前端 未结 1 529
执念已碎
执念已碎 2021-01-22 14:19

I want to update the \"rank\" for a group of MySQL records with sequential numbers using a user-defined variable. The following query runs fine via the MySQL command line:

相关标签:
1条回答
  • 2021-01-22 14:25

    It is not possible to execute multiple statements in one query. Laravel uses PDO under the hood which prevents this. You could attempt to call this over 2 queries instead, since @rank should be available for the duration of the connection.

    DB::query("SET @rank:=0");
    DB::query("UPDATE scores SET rank=@rank:=@rank+1 WHERE game_id=? ORDER BY score DESC", array(4));
    
    0 讨论(0)
提交回复
热议问题