首页 > 解决方案 > 如何在yii2中使用算术运算符选择使用模型名称的查询?

问题描述

我的桌子:- 帐户

| id | total_due | balance_amount |
| -- | --------- | -------------  |
|  1 | 200       | 100            |
|  2 | 400       | 50             |

我想从帐户表中找到支付的金额。我的模型是帐户。

用于查找 padi 金额的 postgres 查询是

$query = "SELECT total_due - balance_amount as paid from account order by total_due - balance_amount desc";

我上面查询的 Yii 代码是

Account::find()->select(['total_due - balance_amount as paid'])->orderBy(['total_due - balance_amount'] => SORT_DESC])->all(); 

它显示错误

“total_due - balance_amount”列不存在

标签: postgresqlyii2

解决方案


 Account::find()->select(['total_due - balance_amount as paid'])->orderBy(['paid' => SORT_DESC])->all()

推荐阅读