yii2 获取最后的查询语句 分页 子查询


$commandQuery = clone $api;
echo $commandQuery->createCommand()->getRawSql(); exit();
echo $model->find()->createCommand()->getRawSql();exit();
使用左连接以及使用in方法
$data = explode(',', $userid);
$userSign = UserSign::find()
->select([ 'off_user_sign.id as id' ,'off_user_sign.user as user','off_user_sign.type as type','off_user_sign.time as time','off_user.name as name'])
->from('off_user_sign')
->where(['off_user_sign.user' => $data ])
->leftJoin('off_user', 'off_user_sign.user = off_user.id')
->asArray()->all();

//进行分页查询
$data = explode(',', $userid);
$query = UserSign::find()
->select([ 'off_user_sign.id as id' ,'off_user_sign.user as user','off_user_sign.type as type','off_user_sign.time as time','off_user.name as name'])
->from('off_user_sign')
->where(['off_user_sign.user' => $data ])
->leftJoin('off_user', 'off_user_sign.user = off_user.id');
//进行分页查询
$pagination = new Pagination([
'params'=>['page'=>$page],
'defaultPageSize' => 20,
'totalCount' => $query->count(),
]);
$userSign = $query->offset($pagination->offset)
->limit($pagination->limit)
->asArray()->orderBy('time desc')->all();

//下面的是对的 可以打印sql语句 以及子查询运用
$subQuery = (new \yii\db\Query())->select(' GROUP_CONCAT(id) ')->from(UserDepartment::tableName())->where(['is_select'=>1]);
$userData = User::find()
->select(' GROUP_CONCAT(username) as usernames ')
->where(['department_id'=>$subQuery])

$commandQuery = clone $userData;
echo $commandQuery->createCommand()->getRawSql();

声明:祁志飞||小祁的博客|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - yii2 获取最后的查询语句 分页 子查询


QQ:137867590