什么是yii2中的等价查询

时间:2017-07-07 09:31:07

标签: yii2

由于在yii2 searchmodel中使用findbysql时搜索过滤不起作用,所以我想在Yii2中写一个等效的“SELECT * FROM challan WHERE id IN (SELECT MAX(id) FROM challan GROUP BY sid)”查询

2 个答案:

答案 0 :(得分:0)

$query->andWhere(new Expression('id IN (SELECT MAX(id) FROM challan GROUP BY sid)'));

答案 1 :(得分:0)

我花了很多时间后才找到答案,而这里是

Challan::find()->Where(['challan.id' => Challan::find()->select(['MAX(id)'])->groupBy('sid')]);