Query Builder

  1. $rows = (new \yii\db\Query())
  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);
  1. use yii\db\Query;
  2. $query = (new Query())
  3. ->from('user')
  4. ->orderBy('id');

SELECT

  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

  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

  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

  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

  1. $query->filterWhere([
  2. 'username' => $username,
  3. 'email' => $email,
  4. ]);
  5. //如果email为空,则 WHERE username=:username
 

ORDER BY

  1. $query->orderBy([
  2. 'id' => SORT_ASC,
  3. 'name' => SORT_DESC,
  4. ]);
  5. //orderBy , addOrderBy


GROUP BY

  1. $query->groupBy('id, status');
  2. $query->addGroupBy(['created_at', 'updated_at']);


HAVING 

  1. $query->having(['status' => $status]);
  2. //having,andHaving,orHaving

LIMIT OR OFFSET

  1. $query->limit(10);
  2. $query->offset(10);

JOIN

  • innerJoin()
  • leftJoin()
  • rightJoin()
  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

  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 为字段或表达式
  1. $count = (new \yii\db\Query())
  2. ->from('user')
  3. ->where(['last_name' => 'Smith'])
  4. ->count();
  5. //SELECT COUNT(*) FROM `user` WHERE `last_name`=:last_name
  6. $command = (new \yii\db\Query())
  7. ->select(['id', 'email'])
  8. ->from('user')
  9. ->where(['last_name' => 'Smith'])
  10. ->limit(10)
  11. ->createCommand();
  12. // show the SQL statement
  13. echo $command->sql;
  14. // show the parameters to be bound
  15. print_r($command->params);
  16. // returns all rows of the query result
  17. $rows = $command->queryAll();
 
QUERY RESULTS
  1. use yii\db\Query;
  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
  1. use yii\db\Query;
  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. }

Yii2 数据操作Query Builder查询数据的更多相关文章

  1. Yii2 数据操作Query Builder

    转载地址: http://blog.csdn.net/hzqghost/article/details/44117081 Yii2 数据操作Query Builder 分类: Yii22015-03- ...

  2. [moka同学笔记]Yii2 数据操作Query Builder 2

    Query Builder $rows = (new \yii\db\Query()) ->select(['dyn_id', 'dyn_name']) ->from('zs_dynast ...

  3. [moka同学笔记]Yii2 数据操作Query Builder

    Query Builder [php] view plain copy   $rows = (new \yii\db\Query()) ->select(['dyn_id', 'dyn_name ...

  4. Yii2 数据操作Query Builder(转)

    Query Builder $rows = (new \yii\db\Query()) ->select(['dyn_id', 'dyn_name']) ->from('zs_dynast ...

  5. Studio 3T 如何使用 Query Builder 查询数据

    Studio 3T 是一款对 MongoDB 进行数据操作的可视化工具. 在 Studio 3T 中,我们可以借助 Query Builder 的 Drag & Drop 来构建查询条件. 具 ...

  6. mysql 数据操作 单表查询 目录

    mysql 数据操作 单表查询 mysql 数据操作 单表查询 简单查询 避免重复DISTINCT mysql 数据操作 单表查询 通过四则运算查询 mysql 数据操作 单表查询 concat()函 ...

  7. mysql 数据操作 多表查询 目录

    mysql 数据操作 多表查询 准备 多表连接查询介绍 mysql 数据操作 多表查询 多表连接查询 笛卡尔积 mysql 数据操作 多表查询 多表连接查询 内连接 mysql 数据操作 多表查询 多 ...

  8. mysql 数据操作 单表查询 where 约束 目录

    mysql 数据操作 单表查询 where约束 between and or mysql 数据操作 单表查询 where约束 is null in mysql 数据操作 单表查询 where约束 li ...

  9. mysql 数据操作 单表查询 group by 分组 目录

    mysql 数据操作 单表查询 group by 介绍 mysql 数据操作 单表查询 group by 聚合函数 mysql 数据操作 单表查询 group by 聚合函数 没有group by情况 ...

随机推荐

  1. CSS----盒子模型与浮动

     盒模型(框模型) 页面上任何一个元素我们都可以看成是一个盒子,盒子会占用一定的空间和位置他们之间相互制约,就形成了网页的布局 w3c的盒模型的构成:content border padding ma ...

  2. Ambient Light

    [Ambient Light] Ambient light is light that is present all around the scene and doesn’t come from an ...

  3. Unity&UGUI

    UI:User Interface 用户交互接口,处理用户与程序直接的交互 新建一个UI控件,都会同时新建两个物体: --Canvas:画布,所有的UI控件都必须放到画布上 --EnventSyste ...

  4. Python基础之-----------函数

    ---恢复内容开始--- 函数:在其他的语言中,我们也经常听到函数的概念,那么什么是函数呢?在Java中叫做method: 定义:函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函 ...

  5. idea 安装和破解

    https://blog.csdn.net/SmileLvCha/article/details/78936659

  6. The <classpath> or <modulepath> for <junit> must include junit.jar if not in Ant's own classpath

    The <classpath> or <modulepath> for <junit> must include junit.jar if not in Ant's ...

  7. maven项目下出现java.lang.ClassNotFoundException: ContextLoader异常

    原因:出现此异常是因为tomcat的webapp目录下没有lib文件. 解决方案: 1.右键点击项目--选择Properties选择Deployment Assembly,在右边点击Add按钮,在弹出 ...

  8. 六、Prototype 原型设计模式

    需求:使用 new 生成实例需要指定类名,在不指定类的情况下生成实例 代码清单: 原型接口 Product: public interface Product extends Cloneable{ v ...

  9. spring中的bean的属性scope

    spring中bean的scope属性,有如下5种类型: singleton 表示在spring容器中的单例,通过spring容器获得该bean时总是返回唯一的实例 prototype表示每次获得be ...

  10. numpy的使用数组的创建2

    随机创建了长度为十的数组 获得十以类的随机整数 快速获取数组2乘3维的数组 生成20个1到10之间的数组 通过reshape 将这些数变成二位数组 shape这个方法可以查看数组中的元素是几行几列的