php yii2 ActiveRecord 使用计算查找 OrderBy
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34588843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
yii2 ActiveRecord Find OrderBy with calculation
提问by william
Trying to fetch a description from my database. The query returns the result but I would like to order the result to only show the one with the highest vote.
试图从我的数据库中获取描述。查询返回结果,但我想将结果排序为仅显示投票最高的那个。
The vote should be calculated by the upvoted
column subtracted by the downvoted
column
票数应按该upvoted
列减去该downvoted
列计算
$description = UnitDescription::find()
->where(['id_unit' => $model->id])
->orderBy([
'upvoted - downvoted' => SORT_DESC //Need this line to be fixed
])
->one();
I was hoping someone might have a way to write this part of the query - Thanks
我希望有人可能有办法编写查询的这一部分 - 谢谢
回答by soju
You should simply try :
你应该简单地尝试:
$description = UnitDescription::find()
->where(['id_unit' => $model->id])
->orderBy(['(upvoted - downvoted)' => SORT_DESC])
->one();