常用方法如下:
User::find()->all(); 此方法返回所有数据;
User::findOne($id); 此方法返回 主键 id=1 的一条数据(举个例子);
User::find()->where(['name' => '小伙儿'])->one(); 此方法返回 ['name' => '小伙儿'] 的一条数据;
User::find()->where(['name' => '小伙儿'])->all(); 此方法返回 ['name' => '小伙儿'] 的所有数据;
User::find()->orderBy('id DESC')->all(); 此方法是排序查询;
User::findBySql('SELECT * FROM user')->all(); 此方法是用 sql 语句查询 user 表里面的所有数据;
User::findBySql('SELECT * FROM user')->one(); 此方法是用 sql 语句查询 user 表里面的一条数据;
User::find()->andWhere(['sex' => '男', 'age' => '24'])->count('id'); 统计符合条件的总条数;
User::find()->andFilterWhere(['like', 'name', '小伙儿']); 此方法是用 like 查询 name 等于 小伙儿的 数据
User::find()->one(); 此方法返回一条数据;
User::find()->all(); 此方法返回所有数据;
User::find()->count(); 此方法返回记录的数量;
User::find()->average(); 此方法返回指定列的平均值;
User::find()->min(); 此方法返回指定列的最小值 ;
User::find()->max(); 此方法返回指定列的最大值 ;
User::find()->scalar(); 此方法返回值的第一行第一列的查询结果;
User::find()->column(); 此方法返回查询结果中的第一列的值;
User::find()->exists(); 此方法返回一个值指示是否包含查询结果的数据行;
User::find()->batch(10); 每次取 10 条数据
User::find()->each(10); 每次取 10 条数据, 迭代查询
Yii2查询语句转换为MySQL查询语句的方法
$query->createCommand()->getRawSql()
实例:
$query = self::find()->joinWith([//yii2 query 方法
'homework',
'companyEmployee',
'records'
]);
$dataProvider = new ActiveDataProvider([//分页组件
'query' =>$query,
'pagination' => [
'pageSize' => $this->page_size
]
]);
$dataProvider->setSort([
'attributes'=>[
'id',
'view_count'
]
]);
$this->load($param);
$query->andFilterWhere([
'classes_homework.id'=>$this->homework_id
]);
$query->andFilterWhere([
'company_employee.id' => $this->created_by
]);
$query->andFilterWhere([
'company_homework_assign_record.assign_id' => $this->id
]);
$query->andFilterWhere([
'or',
[
'like',
'classes_homework.homework_title',
$this->homework_title
],
[
'like',
'company_employee.realname',
$this->realname
]
]);
$query->andWhere([
'classes_homework.homework_status'=>1,
]);
$query->groupBy(['classes_homework_assign.id']);
$query->orderBy("classes_homework_assign.id desc");
$query->select(['count(IF(classes_homework_assign_record.homework_is_finish = 1,TRUE,NULL)) as finishnum',//4表联查,统计一张表中已完成字段的数量
'COUNT(IF(classes_homework_assign_record.homework_is_comment = 0,TRUE,NULL)) as needRecord',
'classes_homework.homework_title',
'classes_homework_assign.homework_id',
'classes_homework_assign.classes_id',
'company_employee.realname',
'classes_homework_assign.finish_count',
'classes_homework_assign.assign_count',
'classes_homework_assign.id',
'classes_homework_assign.created_at']);
return $dataProvider;
除 分页部分,对应的sql语句如下所示:
SELECT
count(IF(d.homework_is_finish = 1,TRUE,NULL)) as finishnum,
COUNT(IF(d.homework_is_comment = 0,TRUE,NULL)) as needRecord,
b.homework_title,
c.realname,
a.finish_count,
a.assign_count,
a.id as assign_id,
a.classes_id,
d.homework_id,
a.created_at
FROM classes_homework_assign a
LEFT JOIN classes_homework b ON a.homework_id = b.id
LEFT JOIN company_employee c ON a.created_by= c.id
LEFT JOIN classes_homework_assign_record d ON a.id = d.assign_id
WHERE b.homework_status=1 AND a.classes_id = 217
GROUP BY a.id
ORDER BY a.id DESC