Yii2 数据操作Query Builder
[php] view
plaincopy
[php] view plaincopy
SELECT
[php] view plaincopy
FORM
[php] view plaincopy
WHERE
[php] view plaincopy
ADD WHERE
[php] view plaincopy
FILTER WHERE
[php] view plaincopy
ORDER BY [php] view plaincopy
GROUP BY [php] view plaincopy
HAVING
[php] view plaincopy
LIMIT OR OFFSET
[php] view plaincopy
JOIN

UNION [php] view plaincopy
QUERY METHODS
QUERY RESULTS
[php] view plaincopy
INDEXING
[php] view plaincopy

- $rows = (new yiidbQuery())
- ->select(["dyn_id", "dyn_name"])
- ->from("zs_dynasty")
- ->where(["between","dyn_id", 1,30])
- ->limit(10)
- ->all();
- print_r($rows);
[php] view plaincopy

- use yiidbQuery;
- $query = (new Query())
- ->from("user")
- ->orderBy("id");
SELECT
[php] view plaincopy

- $query->select("*")->
- select("dyn_id as id, dynasty.dyn_name")->
- $query->select(["dyn_id as id", "CONCAT(dyn_name,"a")"])->
- $query->select("user_id")->distinct()->
FORM
[php] view plaincopy

- $query->from("user");
- $query->from(["public.user u", "public.post p"]);
- $query->from("public.user u, public.post p");
- $query->from(["u" => "public.user", "p" => "public.post"]);
- ----------
- $subQuery = (new Query())->select("id")->from("user")->where("status=1");
- // SELECT * FROM (SELECT `id` FROM `user` WHERE status=1) u
- $query->from(["u" => $subQuery]);
WHERE
[php] view plaincopy

- where("status=1")->
- where("status=:status", [":status" => $status])->
- where([
- "status" => 10,
- "type" => null,
- "id" => [4, 8, 15],
- ])->
- -------
- $userQuery = (new Query())->select("id")->from("user");
- // ...WHERE `id` IN (SELECT `id` FROM `user`)
- $query->...->where(["id" => $userQuery])->...
- --------
- ["and", "id=1", "id=2"] //id=1 AND id=2
- ["and", "type=1", ["or", "id=1", "id=2"]] //type=1 AND (id=1 OR id=2)
- ["between", "id", 1, 10] //id BETWEEN 1 AND 10
- ["not between", "id", 1, 10] //not id BETWEEN 1 AND 10
- ["in", "id", [1, 2, 3]] //id IN (1, 2, 3)
- ["not in", "id", [1, 2, 3]] //not id IN (1, 2, 3)
- ["like", "name", "tester"] //name LIKE "%tester%"
- ["like", "name", ["test", "sample"]] //name LIKE "%test%" AND name LIKE "%sample%"
- ["not like", "name", ["or", "test", "sample"]] //not name LIKE "%test%" OR not name LIKE "%sample%"
- ["exists","id", $userQuery] //EXISTS (sub-query) | not exists
- [">", "age", 10] //age>10
ADD WHERE
[php] view plaincopy

- $status = 10;
- $search = "yii";
- $query->where(["status" => $status]);
- if (!empty($search)) {
- $query->andWhere(["like", "title", $search]);
- }
- //WHERE (`status` = 10) AND (`title` LIKE "%yii%")
- //andWhere() or orWhere()
FILTER WHERE
[php] view plaincopy

- $query->filterWhere([
- "username" => $username,
- "email" => $email,
- ]);
- //如果email为空,则 WHERE username=:username
ORDER BY [php] view plaincopy

- $query->orderBy([
- "id" => SORT_ASC,
- "name" => SORT_DESC,
- ]);
- //orderBy , addOrderBy
GROUP BY [php] view plaincopy

- $query->groupBy("id, status");
- $query->addGroupBy(["created_at", "updated_at"]);
HAVING
[php] view plaincopy

- $query->having(["status" => $status]);
- //having,andHaving,orHaving
LIMIT OR OFFSET
[php] view plaincopy

- $query->limit(10);
- $query->offset(10);
JOIN
- innerJoin()
- leftJoin()
- rightJoin()

- $query->select(["user.name AS author", "post.title as title"])
- ->from("user")
- ->leftJoin("post", "post.user_id = user.id");
- $query->join("FULL OUTER JOIN", "post", "post.user_id = user.id");
- $query->leftJoin(["u" => $subQuery], "u.id=author_id");
UNION [php] view plaincopy

- $query = new Query();
- $query->select("id, category_id as type, name")->from("post")->limit(10);
- $anotherQuery = new Query();
- $anotherQuery->select("id, type, name")->from("user")->limit(10);
- $query->union($anotherQuery);
QUERY METHODS
- all() //所有行列
- one() //第一行
- column() //第一列
- scalar() //第一行第一列
- exists() //是否有结果存在
- count() //记录数量
- sum($q), average($q), max($q), min($q) //$q 为字段或表达式

- $count = (new yiidbQuery())
- ->from("user")
- ->where(["last_name" => "Smith"])
- ->count();
- //SELECT COUNT(*) FROM `user` WHERE `last_name`=:last_name
- $command = (new yiidbQuery())
- ->select(["id", "email"])
- ->from("user")
- ->where(["last_name" => "Smith"])
- ->limit(10)
- ->createCommand();
- // show the SQL statement
- echo $command->sql;
- // show the parameters to be bound
- print_r($command->params);
- // returns all rows of the query result
- $rows = $command->queryAll();
QUERY RESULTS
[php] view plaincopy

- use yiidbQuery;
- $query = (new Query())
- ->from("user")
- ->indexBy("username");
- foreach ($query->batch() as $users) {
- // $users is indexed by the "username" column
- }
- foreach ($query->each() as $username => $user) {
- }
INDEXING
[php] view plaincopy

- use yiidbQuery;
- $query = (new Query())
- ->from("user")
- ->orderBy("id");
- foreach ($query->batch() as $users) {
- // batch( $batchSize = 100, $db = null )
- // 一个批次取100行
- }
- foreach ($query->each() as $user) {
- // 一行一行取
- }
查询
[php] view plaincopy
- //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);
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: 磁盘IO高原因查找
- 下一篇: golang类型转换