两表连查
$model = Article::find()->joinWith(['type'])->select('new,t_name,article.t_id')->asArray()->all(); print_r($model);
use yii\db\Query;
$top10 = (new Query())
->from('article')
->select(['created_at','title'])
->groupBy(['created_at'])
->orderBy('id DESC')
->limit(10)
->all();
//参数绑定,查询操作
$sql='SELECT c.id,c.name,c.phone,c.age,c.sex,c.birthday,a.parent_id,a.camp_id,a.is_uppic,a.is_comment FROM `'.ChildrenApply::getTableSchema()->name.'` AS a left JOIN `'.Children::getTableSchema()->name.'` AS c ON c.`id`=a.`children_id` WHERE a.`parent_id`=:parent_id AND a.`camp_id`=:camp_id and a.status=1';
$res=Yii::$app->db->createCommand($sql)->bindValues([':parent_id' => $parent_id,':camp_id' => $camp_id])->queryAll();
插入操作
$res= Yii::$app->db->createCommand()->batchInsert(Children::tableName(), $dbKey, $dbValue)->execute();
//自增和自减
User::updateAllCounters([
'point'
=> 8],
'id=1'
);
User::updateAllCounters([
'point'
=>-8],
'id=1'
);
//关联分页查询
$data = $query->orderBy('apply.created_at desc') ->leftJoin('party','party.id=apply.party_id') ->select('apply.*,party.site') ->offset($pagination->offset) ->limit($pagination->limit) ->where(['apply.user_id' => $user_id]) ->asArray() ->all();