1.简单查询:
]: 根据查询结果返回查询的第一条记录。
[[all()]]: 根据查询结果返回所有记录。
[[count()]]: 返回记录的数量。
[[sum()]]: 返回指定列的总数。
[[average()]]: 返回指定列的平均值。
[[min()]]: 返回指定列的最小值。
[[max()]]: 返回指定列的最大值。
[[scalar()]]: 返回查询结果的第一行中的第一列的值。
[[column()]]: 返回查询结果中的第一列的值。
[[exists()]]: 返回一个值,该值指示查询结果是否有数据。
[[where()]]: 添加查询条件
[[with()]]: 该查询应执行的关系列表。
[[indexby()]]: 根据索引的列的名称查询结果。
[[asarray()]]: 以数组的形式返回每条记录。
[code=php]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()->asarray()->one();    以数组形式返回一条数据;  
  
customer::find()->asarray()->all();    以数组形式返回所有数据;
customer::find()->where($condition)->asarray()->one();    根据条件以数组形式返回一条数据;  
  
customer::find()->where($condition)->asarray()->all();    根据条件以数组形式返回所有数据;
customer::find()->where($condition)->asarray()->orderby('id desc')->all();    根据条件以数组形式返回所有数据,并根据id倒序;
2.关联查询:
[[activerecord::hasone()]]:返回对应关系的单条记录
[[activerecord::hasmany()]]:返回对应关系的多条记录[/code]
应用实例:
);
    }
     
    public function getcountry()
    {
        //客户和国家是一对一的关系所以用hasone
        return $this->hasone(countrysmodel::classname(), ['id'=>'country_id']);
    }
    ....
}
      
// 查询客户与他们的订单和国家
customermodel::find()->with('orders', 'country')->all();
// 查询客户与他们的订单和订单的发货地址
customermodel::find()->with('orders.address')->all();
// 查询客户与他们的国家和状态为1的订单
customermodel::find()->with([
    'orders' => function ($query) {
        $query->andwhere('status = 1');
        },
        'country',
])->all();
:with中的orders对应getorders
<b>常见问题:</b>
1.在查询时加了->select();如下,要加上order_id,即关联的字段(比如:order_id)比如要在select中,否则会报错:undefined index order_id
// 查询客户与他们的订单和国家
customermodel::find()->select('order_id')->with('orders', 'country')->all();
)->one();  
// 查询年龄为30,状态值为1的客户  
$customer = customer::findone(['age' => 30, 'status' => 1]);  
$customer = customer::find()->where(['age' => 30, 'status' => 1])->one();  
// 查询key值为10的所有客户  
$customers = customer::findall(10);  
$customers = customer::find()->where(['id' => 10])->all();  
// 查询key值为10,11,12的客户  
$customers = customer::findall([10, 11, 12]);  
$customers = customer::find()->where(['id' => [10, 11, 12]])->all();  
// 查询年龄为30,状态值为1的所有客户  
$customers = customer::findall(['age' => 30, 'status' => 1]);  
$customers = customer::find()->where(['age' => 30, 'status' => 1])->all();  
  
<b>where()条件:</b>  
  
$customers = customer::find()->where($cond)->all();   
  
$cond写法举例:  
  
// sql: (type = 1) and (status = 2).  
$cond = ['type' => 1, 'status' => 2]   
  
// sql:(id in (1, 2, 3)) and (status = 2)  
$cond = ['id' => [1, 2, 3], 'status' => 2]   
  
//sql:status is null  
$cond = ['status' => null]  
  
<b>[[]]</b>:将不同的条件组合在一起,用法举例:  
  
//sql:`id=1 and id=2`  
$cond = ['and', 'id=1', 'id=2']  
  
//sql:`type=1 and (id=1 or id=2)`  
$cond = ['and', 'type=1', ['or', 'id=1', 'id=2']]
[[or]]:
], ['id' => [1, 2, 3]]  
  
<b>[[]]:</b>  
  
//sql:`not (attribute is null)`  
$cond = ['not', ['attribute' => null]]
[[between]]: not between 用法相同
<b>[[]]: </b>not in 用法类似  
  
//sql:`id in (1, 2, 3)`  
$cond = ['in', 'id', [1, 2, 3]]  
  
//in条件也适用于多字段  
$cond = ['in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]]  
  
//也适用于内嵌sql语句  
$cond = ['in', 'user_id', (new query())->select('id')->from('users')->where(['active' => 1])]
[[like]]:
//sql:`name like '%test%' and name like '%sample%'`  
$cond = ['like', 'name', ['test', 'sample']]  
  
//sql:`name like '%tester'`  
$cond = ['like', 'name', '%tester', false]  
  
<b>[[]]: </b>not exists用法类似  
  
//sql:exists (select "id" from "users" where "active"=1)  
$cond = ['exists', (new query())->select('id')->from('users')->where(['active' => 1])]
此外,您可以指定任意运算符如下
//sql:`id >= 10`
$cond = ['>=', 'id', 10]
//sql:`id != 10`
$cond = ['!=', 'id', 10]
常用查询:
// where admin_id >= 10 limit 0,10
user::find()->select('*')->where(['>=', 'admin_id', 10])->offset(0)->limit(10)->all()
// select `id`, (select count(*) from `user`) as `count` from `post` 
 $subquery = (new query())->select('count(*)')->from('user'); 
 $query = (new query())->select(['id', 'count' => $subquery])->from('post');
// select distinct `user_id` ... 
user::find()->select('user_id')->distinct();
更新:
//update();
//runvalidation boolen 是否通过validate()校验字段 默认为true 
//attributenames array 需要更新的字段 
$model->update($runvalidation , $attributenames); 
//updateall();
//update customer set status = 1 where status = 2
customer::updateall(['status' => 1], 'status = 2'); 
//update customer set status = 1 where status = 2 and uid = 1;
customer::updateall(['status' => 1], ['status'=> '2','uid'=>'1']);
删除:
$model = customer::findone($id);
$model->delete();
$model->deleteall(['id'=>1]);
批量插入:
yii::$app->db->createcommand()->batchinsert(usermodel::tablename(), ['user_id','username'], [
['1','test1'],
['2','test2'],
['3','test3'], 
])->execute();
查看执行sql
//usermodel 
$query = usermodel::find()->where(['status'=>1]); 
echo $query->createcommand()->getrawsql();
以上就是yii2.0数据库操作增删改查详解的内容。
   
 
   