yii2 ActiveRecord Find OrderBy with Computing

2022-08-30 21:01:06

尝试从我的数据库中提取描述。查询返回结果,但我想对结果进行排序,以便仅显示投票率最高的结果。

投票应按列减去该列来计算upvoteddownvoted

$description = UnitDescription::find()
   ->where(['id_unit' => $model->id])
   ->orderBy([
      'upvoted - downvoted' => SORT_DESC //Need this line to be fixed
   ])
   ->one();

我希望有人可能有办法写这部分查询 - 谢谢


答案 1

您应该简单地尝试:

$description = UnitDescription::find()
    ->where(['id_unit' => $model->id])
    ->orderBy(['(upvoted - downvoted)' => SORT_DESC])
    ->one();

答案 2

推荐