使用 abs 函数将 mysql 查询转换为 laravel



我在mysql中有以下查询:

select *  from matches m where pick_score<>0 order by ABS(pick_score) DESC

我想用拉拉维尔写它,我试过了:

$matches = $matches->where('pick_score', '<>', '0');
$matches = $matches->orderByRaw('ABS(pick_score)', 'DESC');

不幸的是,它没有按预期工作。mysql 查询的结果具有以下值:1222.9396、-914.6969、880.8959,laravel结果具有:2.628662、-36.759403、-103.41754 等。 谁能帮我做正确的查询?

当使用 orderByRaw(( 时,第二个参数用于绑定而不是方向。您只需要在实际的 sql 字符串中包含方向:

$matches = DB::table('matches')
->where('pick_score', '<>', 0)
->orderByRaw('ABS(pick_score) DESC')
->get();

最新更新