Yii2 在 where 条件下添加数学

Yii2 Add math in where condition

在用户 table 我有一个专栏调用 "month"。

我要列出所有满足条件的用户:当月-用户月<=2

这是我的代码

$time = new \DateTime('now');
$today = $time->format('m');
$users = Users::find()->where(['<=', 'month' - $today, 2])->all();

但是这段代码是错误的。请帮我解决这个问题。

希望这一切都有意义。

谢谢!

在 Yii2 中你可以使用不同的格式来构建 where 条件 对于这种情况,使用带有参数

的字符串格式很有用

在字符串格式中,您可以通过这种方式传递文字字符串和参数

$users = Users::find()->where('(month - :today ) <= 2' , [':today'=>$today])->all();

查看更多内容http://www.yiiframework.com/doc-2.0/yii-db-query.html#where()-detail