在status
为abanadoned
或completed
的计划中查找已保存金额的正确语法是什么:
$total = self::sum([
'column' => 'amount_saved_so_far',
'conditions' => 'status = "completed" AND user_id = :user_id:',
//find plans with a status of abandoned as well
'bind' => ['user_id' => $userId]
]);
答案 0 :(得分:3)
你可以简单地做到,这是一个非常好的解决方案:
$total = self::sum([
'column' => 'amount_saved_so_far',
'conditions' => '(status = "completed" OR status = "abanadoned")
AND user_id = :user_id:',
'bind' => ['user_id' => $userId]
]);
或者另一种选择是使用数组绑定:
$total = self::sum([
'column' => 'amount_saved_so_far',
'conditions' => 'status IN ({vars:array}) AND user_id = :user_id:',
'bind' => [
'vars' => ['completed', 'abanadoned'],
'user_id' => $userId
]
]);
如果你有更多的IN子句值,第二个选项会更优雅,更实用。