gpt4 book ai didi

php - 将原始 SQL 查询更改为 Laravel Query Builder 对象

转载 作者:搜寻专家 更新时间:2023-10-30 20:25:24 24 4
gpt4 key购买 nike

我有这个问题:

public function rank()
{
$sql = "
select id, points, team_name,
(select count(*)
from teams t2
where t2.points > t.points or
(t2.points = t.points and t2.id <= t.id)
) as rank
from teams t
where id = ?;
";
$ranks = DB::select($sql, [$this->id]);
foreach ($ranks as $rank) {
return $rank->rank;
}
}

我想将其更改为 Laravel 查询构建器而不是原始查询,我该怎么做?

最佳答案

这应该有效。

$select_raw = <<<SQL
id, points, team_name,(
select count(*)
from teams t2
where t2.points > t.points or
(t2.points = t.points and t2.id <= t.id)
) as rank
SQL;

$ranks = Team::where('id', $this->id)->selectRaw($select_raw)->get();

关于php - 将原始 SQL 查询更改为 Laravel Query Builder 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37098146/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com