zoukankan      html  css  js  c++  java
  • yii框架对数据库查询访问处理

    ## 单表查询
    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(true)->one(); 根据条件以数组形式返回一条数据;(默认是true)
    Customer::find()->where($condition)->asArray(false)->all(); 根据条件以非数组形式返回所有数据;
    Customer::find()->select()->where()->orderBy('id DESC')->limit(1)->asArray()->all();

    eg:
    $res = UserFirstTimeOpenApp::find()->all();
    $res = UserFirstTimeOpenApp::find()->one();
    $res = UserFirstTimeOpenApp::find()->count();|| $res = UserFirstTimeOpenApp::find()->count('id');(个人觉得,这样子或许更快哦)
    $res = UserFirstTimeOpenApp::find()->average('id');
    $res = UserFirstTimeOpenApp::find()->min('id');
    $res = UserFirstTimeOpenApp::find()->max('id');
    $res = UserFirstTimeOpenApp::find()->scalar();
    $res = UserFirstTimeOpenApp::find()->column();
    $res = UserFirstTimeOpenApp::find()->exists();
    $res = UserFirstTimeOpenApp::find()->asArray()->one();
    $res = UserFirstTimeOpenApp::find()->asArray(false)->all();
    $res = UserFirstTimeOpenApp::find()->where(['device_id'=>1111])->orderBy('id DESC')->limit(1)->asArray()->all();
    $res = UserFirstTimeOpenApp::find()->where(['device_id'=>1111])->exists();
    $res = UserFirstTimeOpenApp::find()->select('is_first_day_use')->where(['device_id'=>1111])->orderBy('id DESC')->limit(1)->asArray()->all();

    * where 条件详细学习
    where 条件有三种格式--字符串格式,哈希格式,操作符格式
    1. 字符串基本可以满足所有状况
    $userDecrorationInfoList = UserDecrorationList::find()->where("uid=:uid and type=:type and expire_time>:expire_time",[":uid"=>$uid, "type"=>$type, ":expire_time"=>$cur_date])->asArray()->all();

    2. 哈希格式--相对单调
    $res = UserFirstTimeOpenApp::find()->where(['device_id'=>1111])->asArray()->all();

    3. 操作符格式--基本可以满足所有的状况,很多时候结合哈希格式一起使用
    $uid_array = FamilyMember::find('uid')->where(['family_id'=>$family_id])
    ->andWhere(['<>','uid',$uid])->asArray()->all();

    ## 更新和写入
    $model = new MatchUserBuyCount();
    $res = $model->find()->where(['uid'=>$uid])->asArray()->one();
    $cur_time = date('Y-m-d H:i:s');
    if($res){
    //数据库的更新操作
    $model::updateAll([
    'buy_count' => new Expression("buy_count + {$buy_count}"),
    'update_time' => $cur_time
    ], ['uid'=>$uid]);
    }else{
    //数据库的写入操作
    $model->uid = $uid;
    $model->buy_count = $buy_count;
    $model->start_time = $cur_time;
    $model->update_time = $cur_time;
    $model->save();
    }

  • 相关阅读:
    elasticsearch中多个字段聚合及java实现
    elasticsearch中must和should组合查询
    Hash(哈希/散列)表中冲突处理及命中计算
    PHP代码审计理解(一)----Metinfo5.0变量覆盖
    SSL 3.0 POODLE攻击信息泄露漏洞_CVE-2014-3566
    菜鸡试飞----SRCの信息收集手册
    python3-邮件发送-不同格式
    windows下常用快捷指令记忆
    杂记
    偶然碰到的编码转换技巧--叮!
  • 原文地址:https://www.cnblogs.com/starmoon2019/p/9145330.html
Copyright © 2011-2022 走看看