我想将SQL查询更改为Laravel。 但这会出错。请帮助如何更改此查询。
(此查询进行排名查询。)
select school_id, id, month, ifnull(avg,0) as avg, ifnull(sum,0) as sum,
@rank := @rank +1 as rank from v2_rank_status as s, (select @rank :=0 ) as r
where month ='201901' order by sum desc
答案 0 :(得分:0)
使用前,您需要定义@rank
;
DB::statement('SET @rank = 0'); //we start from 0 because the first query will have +1
DB::select('SELECT school_id, id, month, IFNULL(avg, 0) AS avg, IFNULL(sum,0) AS sum, @rank:=(@rank+1) AS rank FROM v2_rank_status WHERE month = ? order by sum desc', [
'201901'
]);
这会将@rank
设置为0,并以+1递增选择