您好,登錄后才能下訂單哦!
//1.簡單查詢 $admin=Admin::model()->findAll($condition,$params); $admin=Admin::model()->findAll("username=:name",array(":name"=>$username)); $infoArr= NewsList::model()->findAll("status = '1' ORDER BY id DESC limit 10 "); //2. findAllByPk(該方法是根據主鍵查詢一個集合,可以使用多個主鍵) $admin=Admin::model()->findAllByPk($postIDs,$condition,$params); $admin=Admin::model()->findAllByPk($id,"name like :name and age=:age",array(':name'=>$name,'age'=>$age)); $admin=Admin::model()->findAllByPk(array(1,2)); //3.findAllByAttributes (該方法是根據條件查詢一個集合,可以是多個條件,把條件放到數組里面) $admin=Admin::model()->findAllByAttributes($attributes,$condition,$params); $admin=Admin::model()->findAllByAttributes(array('username'=>'admin')); //4.findAllBySql (該方法是根據SQL語句查詢一個數組) $admin=Admin::model()->findAllBySql($sql,$params); $admin=Admin::model()->findAllBySql("select * from admin where username like :name",array(':name'=>'%ad%')); 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()->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 條數據, 迭代查詢 二、查詢對象的方法 //根據主鍵查詢出一個對象,如:findByPk(1); $admin=Admin::model()->findByPk($postID,$condition,$params); $admin=Admin::model()->findByPk(1); //根據一個條件查詢出一組數據,可能是多個,但是他只返回第一行數據 $row=Admin::model()->find($condition,$params); $row=Admin::model()->find('username=:name',array(':name'=>'admin')); //該方法是根據條件查詢一組數據,可以是多個條件,把條件放到數組里面,查詢的也是第一條數據 $admin=Admin::model()->findByAttributes($attributes,$condition,$params); $admin=Admin::model()->findByAttributes(array('username'=>'admin')); //該方法是根據SQL語句查詢一組數據,他查詢的也是第一條數據 $admin=Admin::model()->findBySql($sql,$params); $admin=Admin::model()->findBySql("select * from admin where username=:name",array(':name'=>'admin')); //拼一個獲得SQL的方法,在根據find查詢出一個對象 $criteria=newCDbCriteria; $criteria->select='username';// only select the 'title' column $criteria->condition='username=:username'; //請注意,這是一個查詢的條件,且只有一個查詢條件.多條件用addCondition $criteria->params=array(":username=>'admin'"); $criteria->order ="id DESC"; $criteria->limit ="3"; $post=Post::model()->find($criteria);// $params isnot needed //多條件查詢的語句 $criteria= new CDbCriteria; $criteria->addCondition("id=1");//查詢條件,即where id = 1 $criteria->addInCondition('id',array(1,2,3,4,5));//代表where id IN (1,2,3,4,5,); $criteria->addNotInCondition('id',array(1,2,3,4,5));//與上面正好相法,是NOT IN $criteria->addCondition('id=1','OR');//這是OR條件,多個條件的時候,該條件是OR而非AND $criteria->addSearchCondition('name','分類');//搜索條件,其實代表了。。where name like '%分類%' $criteria->addBetweenCondition('id', 1, 4);//between 1 and 4 $criteria->compare('id', 1); //這個方法比較特殊,他會根據你的參數自動處理成addCondition或者addInCondition. $criteria->compare('id',array(1,2,3)); //即如果第二個參數是數組就會調用addInCondition $criteria->select ='id,parentid,name';//代表了要查詢的字段,默認select='*'; $criteria->join ='xxx'; //連接表 $criteria->with ='xxx'; //調用relations $criteria->limit = 10; //取1條數據,如果小于0,則不作處理 $criteria->offset = 1; //兩條合并起來,則表示 limit 10 offset 1,或者代表了。limit 1,10 $criteria->order ='xxx DESC,XXX ASC' ;//排序條件 $criteria->group ='group 條件'; $criteria->having ='having 條件 '; $criteria->distinct = FALSE;//是否唯一查詢 三、查詢個數,判斷查詢是否有結果 //該方法是根據一個條件查詢一個集合有多少條記錄,返回一個int型數字 $n=Post::model()->count($condition,$params); $n=Post::model()->count("username=:name",array(":name"=>$username)); //該方法是根據SQL語句查詢一個集合有多少條記錄,返回一個int型數字 $n=Post::model()->countBySql($sql,$params); $n=Post::model()->countBySql("select * from admin where username=:name",array(':name'=>'admin')); //該方法是根據一個條件查詢查詢得到的數組有沒有數據,如果有數據返回一個true,否則沒有找到 $exists=Post::model()->exists($condition,$params); $exists=Post::model()->exists("name=:name",array(":name"=>$username)); 四、新增 $admin= new Admin; $admin->username =$username; $admin->password =$password; if($admin->save() > 0){echo "添加成功"; }else{echo "添加失敗"; } 五、修改 Post::model()->updateAll($attributes,$condition,$params); $count=Admin::model()->updateAll(array('username'=>'11111','password'=>'11111'),'password=:pass',array(':pass'=>'1111a1')); if($count> 0){ echo "修改成功"; }else{echo "修改失敗"; } $rt= PostList::model()->updateAll(array('status'=>'1'),'staff_id=:staff AND host_id=:host',array(':staff'=>$staff_id,':host'=>$host_id)); //$pk主鍵,可以是一個也可以是一個集合,$attributes是要修改的字段的集合,$condition條件,$params傳入的值 Post::model()->updateByPk($pk,$attributes,$condition,$params); $count=Admin::model()->updateByPk(1,array('username'=>'admin','password'=>'admin')); $count=Admin::model()->updateByPk(array(1,2),array('username'=>'admin','password'=>'admin'),'username=:name',array(':name'=>'admin')); if($count>0){echo "修改成功"; }else{echo "修改失敗"; } Post::model()->updateCounters($counters,$condition,$params); $count=Admin::model()->updateCounters(array('status'=>1),'username=:name',array(':name'=>'admin')); if($count> 0){ echo "修改成功"; }else{echo "修改失敗"; } //array('status'=>1)代表數據庫中的admin表根據條件username='admin',查詢出的所有結果status字段都自加1 六、刪除 //deleteAll Post::model()->deleteAll($condition,$params); $count= Admin::model()->deleteAll('username=:name and password=:pass',array(':name'=>'admin',':pass'=>'admin')); $count= Admin::model()->deleteAll('id in("1,2,3")');//刪除id為這些的數據 if($count>0){echo"刪除成功"; }else{echo "刪除失敗"; } //deleteByPk Post::model()->deleteByPk($pk,$condition,$params); $count= Admin::model()->deleteByPk(1); $count=Admin::model()->deleteByPk(array(1,2),'username=:name',array(':name'=>'admin')); if($count>0){echo "刪除成功"; }else{echo "刪除失敗"; } //createCommand(執行原生的SQL語句) $sql= "SELECT u.account,i.* FROM sys_user as u left join user_info as i on u.id=i.user_id"; $rows=Yii::$app->db->createCommand($sql)->query(); foreach($rows as $k => $v){ echo$v['add_time']; } 查詢返回多行: $command = $connection->createCommand('SELECT * FROM post'); $posts = $command->queryAll(); 返回單行: $command = $connection->createCommand('SELECT * FROM post WHERE id=1'); $post = $command->queryOne(); 查詢多行單值: $command = $connection->createCommand('SELECT title FROM post'); $titles = $command->queryColumn(); 查詢標量值/計算值: $command = $connection->createCommand('SELECT COUNT(*) FROM post'); $postCount = $command->queryScalar(); $command = $connection->createCommand('UPDATE post SET status=1 WHERE id=1'); $command->execute(); $connection->createCommand()->insert('user', [ 'name' => 'Sam', 'age' => 30, ])->execute(); // INSERT 一次插入多行 $connection->createCommand()->batchInsert('user', ['name', 'age'], [ ['Tom', 30], ['Jane', 20], ['Linda', 25], ])->execute(); // UPDATE $connection->createCommand()->update('user', ['status' => 1], 'age > 30')->execute(); // DELETE $connection->createCommand()->delete('user', 'status = 0')->execute(); //事務的基本結構(多表更新插入操作請使用事務處理) $dbTrans= Yii::app()->db->beginTransaction(); try{ $post= new Post; $post->'title'= 'Hello dodobook!!!'; if(!$post->save())throw newException("Error Processing Request", 1); $dbTrans->commit(); // $this->_end(0,'添加成功!!!'); }catch(Exception$e){ $dbTrans->rollback(); // $this->_end($e->getCode(),$e->getMessage());
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。