基础查询
Customer::find()->one(); 此方法返回一条数据;
Customer::find()->all(); 此方法返回所有数据;
Customer::find()->count(); 此方法返回记录的数量;
Customer::find()->average(); 此方法返回指定列的平均值;
Customer::find()->min(); 此方法返回指定列的最小值 ;
Customer::find()->max(); 此方法返回指定列的最大值 ;
Customer::find()->scalar(); 此方法返回值的第一行第一列的查询结果;
Customer::find()->column(); 此方法返回查询结果中的第一列的值;
Customer::find()->exists(); 此方法返回一个值指示是否包含查询结果的数据行;
Customer::find()->batch(10); 每次取10条数据
Customer::find()->each(10); 每次取10条数据,迭代查询
//根据sql语句查询:查询name=test的客户
Customer::model()->findAllBySql("select * from customer where name = test");
//根据主键查询:查询主键值为1的数据
Customer::model()->findByPk(1);
//根据条件查询(该方法是根据条件查询一个集合,可以是多个条件,把条件放到数组里面)
Customer::model()->findAllByAttributes([‘username‘=>‘admin‘]);
//子查询
$subQuery = (new Query())->select(‘COUNT(*)‘)->from(‘customer‘);
// SELECT `id`, (SELECT COUNT(*) FROM `customer`) AS `count` FROM `customer`
$query = (new Query())->select([‘id‘, ‘count‘ => $subQuery])->from(‘customer‘);
//关联查询:查询客户表(customer)关联订单表(orders),条件是status=1,客户id为1,从查询结果的第5条开始,查询10条数据
$data = (new Query())
->select(‘*‘)
->from(‘customer‘)
->join(‘LEFT JOIN‘,‘orders‘,‘customer.id = orders.customer_id‘)
->where([‘status‘=>‘1‘,‘customer.id‘=>‘1‘])
->offset(5)
->limit(10)
->all()
直接查询
关联查询
原文:https://www.cnblogs.com/kkdn/p/9303060.html