yii2 new Expression,new Expression,new \yii\db\Expression,yii2 FIND_IN_SET,yii sql,yii\db\Expressionsql
演示一段yii2 關於 new Expression 或者 new \yii\db\Expression 的sql語句yii2
首先yii
use yii\db\Expression;
$params = ['1','2'];
$query = Model::find();
$where = ['or'];
$query->where('1=1');
foreach($params as $v){
$where[] = new Expression('FIND_IN_SET(:field, field)',[':field'=>$v]);
}
$query->andWhere($where)->asArray();
echo $query->createCommand()->getRawSql();
輸出sql語句:字符串
SELECT * from `table` where 1=1 AND ((FIND_IN_SET('2', field)) OR (FIND_IN_SET('2', field)));
修改:get
//切記 字符串中包含變量用雙引號"",否則變量不會解析。
$i = 0;
foreach($params as $v){
$where[] = new Expression("FIND_IN_SET(:field_$i, field)",[":field_$i"=>$v]);
}
//切記 字符串中包含變量用雙引號"",否則變量不會解析。io
再次輸出sql語句:table
SELECT * from `table` where 1=1 AND ((FIND_IN_SET('1', field)) OR (FIND_IN_SET('2', field)));
變量