如何在laravel查询构建器中执行此操作? (这基本上可以获得你在投票系统中的当前位置)
SELECT position FROM
(SELECT participant.target_user_id, @rownum := @rownum + 1 as position FROM
(SELECT target_user_id, count(*) as votes FROM contest_participants_votes GROUP BY
target_user_id ORDER BY votes DESC) as participant
JOIN (SELECT @rownum := 0) r) x
WHERE target_user_id = 1
答案 0 :(得分:0)
我们在占位符
中使用了这样的原始语句$position = \DB::select(\DB::raw(
'SELECT position FROM
(SELECT participant.target_user_id, @rownum := @rownum + 1 as position FROM
(SELECT target_user_id, count(*) as votes FROM contest_participants_votes GROUP BY
target_user_id ORDER BY votes DESC) as participant
JOIN (SELECT @rownum := 0) r) x
WHERE target_user_id = ?'), [$pid]);