from("zs_dynasty")       ->where(["between","" />
牛骨文教育服务平台(让学习变的简单)
博文笔记

Yii2 数据操作Query Builder

创建时间:2015-11-10 投稿人: 浏览次数:1195
[php] view plaincopy
  1. $rows = (new yiidbQuery())  
  2.     ->select(["dyn_id", "dyn_name"])  
  3.     ->from("zs_dynasty")  
  4.     ->where(["between","dyn_id", 1,30])  
  5.     ->limit(10)  
  6.     ->all();  
  7. print_r($rows);  

[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. use yiidbQuery;  
  2. $query = (new Query())  
  3.     ->from("user")  
  4.     ->orderBy("id");  

SELECT
[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. $query->select("*")->  
  2. select("dyn_id as id, dynasty.dyn_name")->  
  3. $query->select(["dyn_id as id", "CONCAT(dyn_name,"a")"])->  
  4. $query->select("user_id")->distinct()->  



FORM
[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. $query->from("user");  
  2. $query->from(["public.user u", "public.post p"]);  
  3. $query->from("public.user u, public.post p");  
  4. $query->from(["u" => "public.user", "p" => "public.post"]);  
  5. ----------  
  6. $subQuery = (new Query())->select("id")->from("user")->where("status=1");  
  7. // SELECT * FROM (SELECT `id` FROM `user` WHERE status=1) u   
  8. $query->from(["u" => $subQuery]);  

WHERE
[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. where("status=1")->  
  2. where("status=:status", [":status" => $status])->  
  3. where([  
  4.     "status" => 10,  
  5.     "type" => null,  
  6.     "id" => [4, 8, 15],  
  7. ])->  
  8. -------  
  9. $userQuery = (new Query())->select("id")->from("user");  
  10. // ...WHERE `id` IN (SELECT `id` FROM `user`)  
  11. $query->...->where(["id" => $userQuery])->...  
  12. --------  
  13. ["and", "id=1", "id=2"] //id=1 AND id=2  
  14. ["and", "type=1", ["or", "id=1", "id=2"]] //type=1 AND (id=1 OR id=2)  
  15. ["between", "id", 1, 10] //id BETWEEN 1 AND 10  
  16. ["not between", "id", 1, 10] //not id BETWEEN 1 AND 10  
  17. ["in", "id", [1, 2, 3]] //id IN (1, 2, 3)  
  18. ["not in", "id", [1, 2, 3]] //not id IN (1, 2, 3)  
  19. ["like", "name", "tester"] //name LIKE "%tester%"  
  20. ["like", "name", ["test", "sample"]] //name LIKE "%test%" AND name LIKE "%sample%"  
  21. ["not like", "name", ["or", "test", "sample"]] //not name LIKE "%test%" OR not name LIKE "%sample%"  
  22. ["exists","id", $userQuery] //EXISTS (sub-query) | not exists  
  23. [">", "age", 10] //age>10  

ADD WHERE
[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. $status = 10;  
  2. $search = "yii";  
  3. $query->where(["status" => $status]);  
  4. if (!empty($search)) {  
  5.     $query->andWhere(["like", "title", $search]);  
  6. }  
  7. //WHERE (`status` = 10) AND (`title` LIKE "%yii%")  
  8. //andWhere() or orWhere()  


FILTER WHERE

[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. $query->filterWhere([  
  2.     "username" => $username,  
  3.     "email" => $email,  
  4. ]);  
  5. //如果email为空,则 WHERE username=:username  

ORDER BY [php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. $query->orderBy([  
  2.     "id" => SORT_ASC,  
  3.     "name" => SORT_DESC,  
  4. ]);  
  5. //orderBy , addOrderBy  

GROUP BY
[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. $query->groupBy("id, status");  
  2. $query->addGroupBy(["created_at", "updated_at"]);  

HAVING 

[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. $query->having(["status" => $status]);  
  2. //having,andHaving,orHaving  

LIMIT OR OFFSET
[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. $query->limit(10);  
  2. $query->offset(10);  

JOIN
  • innerJoin()
  • leftJoin()
  • rightJoin()
[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. $query->select(["user.name AS author", "post.title as title"])  
  2.     ->from("user")  
  3.     ->leftJoin("post", "post.user_id = user.id");  
  4. $query->join("FULL OUTER JOIN", "post", "post.user_id = user.id");  
  5. $query->leftJoin(["u" => $subQuery], "u.id=author_id");  


UNION
[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. $query = new Query();  
  2. $query->select("id, category_id as type, name")->from("post")->limit(10);  
  3. $anotherQuery = new Query();  
  4. $anotherQuery->select("id, type, name")->from("user")->limit(10);  
  5. $query->union($anotherQuery);  

QUERY METHODS
  • all() //所有行列
  • one() //第一行
  • column() //第一列
  • scalar() //第一行第一列
  • exists() //是否有结果存在
  • count() //记录数量
  • sum($q), average($q), max($q), min($q) //$q 为字段或表达式
[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. $count = (new yiidbQuery())  
  2.     ->from("user")  
  3.     ->where(["last_name" => "Smith"])  
  4.     ->count();  
  5. //SELECT COUNT(*) FROM `user` WHERE `last_name`=:last_name  
  6.   
  7.   
  8. $command = (new yiidbQuery())  
  9.     ->select(["id", "email"])  
  10.     ->from("user")  
  11.     ->where(["last_name" => "Smith"])  
  12.     ->limit(10)  
  13.     ->createCommand();  
  14.       
  15. // show the SQL statement  
  16. echo $command->sql;  
  17. // show the parameters to be bound  
  18. print_r($command->params);  
  19.   
  20.   
  21. // returns all rows of the query result  
  22. $rows = $command->queryAll();  

QUERY RESULTS
[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. use yiidbQuery;  
  2. $query = (new Query())  
  3.     ->from("user")  
  4.     ->indexBy("username");  
  5. foreach ($query->batch() as $users) {  
  6.     // $users is indexed by the "username" column  
  7. }  
  8. foreach ($query->each() as $username => $user) {  
  9. }  


INDEXING
[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. use yiidbQuery;  
  2. $query = (new Query())  
  3.     ->from("user")  
  4.     ->orderBy("id");  
  5. foreach ($query->batch() as $users) {  
  6.     // batch( $batchSize = 100, $db = null )  
  7.     // 一个批次取100行  
  8. }  
  9. foreach ($query->each() as $user) {  
  10.     // 一行一行取  
  11. }  

查询

[php] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //1.简单查询  
  2. $admin=Admin::model()->findAll($condition,$params);  
  3. $admin=Admin::model()->findAll("username=:name",array(":name"=>$username));  
  4.    
  5. $infoArr= NewsList::model()->findAll("status = "1" ORDER BY id DESC limit 10 ");  
  6.    
  7. //2. findAllByPk(该方法是根据主键查询一个集合,可以使用多个主键)  
  8. $admin=Admin::model()->findAllByPk($postIDs,$condition,$params);  
  9. $admin=Admin::model()->findAllByPk($id,"name like :name and age=:age",array(":name"=>$name,"age"=>$age));  
  10. $admin=Admin::model()->findAllByPk(array(1,2));  
  11.    
  12. //3.findAllByAttributes (该方法是根据条件查询一个集合,可以是多个条件,把条件放到数组里面)  
  13. $admin=Admin::model()->findAllByAttributes($attributes,$condition,$params);  
  14. $admin=Admin::model()->findAllByAttributes(array("username"=>"admin"));  
  15.    
  16. //4.findAllBySql (该方法是根据SQL语句查询一个数组)  
  17. $admin=Admin::model()->findAllBySql($sql,$params);  
  18. $admin=Admin::model()->findAllBySql("select * from admin where username like :name",array(":name"=>"%ad%"));  
  19. User::find()->all();    此方法返回所有数据;  
  20. User::findOne($id);   此方法返回 主键 id=1  的一条数据(举个例子);   
  21. User::find()->where(["name" => "小伙儿"])->one();   此方法返回 ["name" => "小伙儿"] 的一条数据;  
  22. User::find()->where(["name" => "小伙儿"])->all();   此方法返回 ["name" => "小伙儿"] 的所有数据;  
  23. User::find()->orderBy("id DESC")->all();   此方法是排序查询;  
  24. User::findBySql("SELECT * FROM user")->all();  此方法是用 sql  语句查询 user 表里面的所有数据;  
  25. User::findBySql("SELECT * FROM user")->one();  此方法是用 sql  语句查询 user 表里面的一条数据;  
  26. User::find()->andWhere(["sex" => "男", "age" => "24"])->count("id");   统计符合条件的总条数;  
  27. User::find()->one();    此方法返回一条数据;  
  28. User::find()->all();    此方法返回所有数据;  
  29. User::find()->count();    此方法返回记录的数量;  
  30. User::find()->average();    此方法返回指定列的平均值;  
  31. User::find()->min();    此方法返回指定列的最小值 ;  
  32. User::find()->max();    此方法返回指定列的最大值 ;  
  33. User::find()->scalar();    此方法返回值的第一行第一列的查询结果;  
  34. User::find()->column();    此方法返回查询结果中的第一列的值;  
  35. User::find()->exists();    此方法返回一个值指示是否包含查询结果的数据行;  
  36. User::find()->batch(10);  每次取 10 条数据   
  37. User::find()->each(10);  每次取 10 条数据, 迭代查询  
  38. 二、查询对象的方法  
  39. //根据主键查询出一个对象,如:findByPk(1);  
  40. $admin=Admin::model()->findByPk($postID,$condition,$params);  
  41. $admin=Admin::model()->findByPk(1);  
  42.    
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。