Query Builder

[php] view plain copy

 
  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);
 
[php] view plain copy

 

  1. use yii\db\Query;
  2. $query = (new Query())
  3. ->from('user')
  4. ->orderBy('id');
 

SELECT

 
[php] view plain copy

 

  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 plain copy

 

  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 plain copy

 

  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 plain copy

 

  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 plain copy

 

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

ORDER BY

[php] view plain copy

 

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

GROUP BY

[php] view plain copy

 

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

HAVING 

 
[php] view plain copy

 

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

LIMIT OR OFFSET

 
[php] view plain copy

 

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

JOIN

 
  • innerJoin()
  • leftJoin()
  • rightJoin()
[php] view plain copy

 

  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 plain copy

 

  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 plain copy

 

  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

 
[php] view plain copy

 

  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

 
[php] view plain copy

 

  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. }
 
 
 
 

[moka同学笔记]Yii2 数据操作Query Builder的更多相关文章

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

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

  2. Yii2 数据操作Query Builder

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

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

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

  4. Yii2 数据操作Query Builder查询数据

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

  5. [moka同学笔记]yii2.0查询数据库

      一. [:id占位符]使用 $results = Test::findBySql($sql,array(':id'=>'1 or 1=1))->all()   二. [id=1]  选 ...

  6. [moka同学笔记]yii2.0数据库操作以及分页

    1.model中models/article.php 1 <?php 2 3 namespace app\models; 4 5 use Yii; 6 7 /** 8 * This is the ...

  7. [moka同学笔记]yii2.0缓存

    1.控制器中CacheDemoController.php <?php /** * Created by PhpStorm. * User: moka同学 * Date: 2016/06/29 ...

  8. [moka同学笔记]Yii2.0 modal的使用

    第一次使用,时候不明白什么原理,大概用了几次后,才模模糊糊搞清楚原来是怎么一回事,现在就把写过的代码,贴在下边. 1.在视图文件中, 第一步首先在index.php文件中 做了一个a链接的按钮 调用了 ...

  9. [moka同学笔记]yii2.0表单的使用

    1.创建model   /biaodan.php <?php /** * Created by PhpStorm. * User: moka同学 * Date: 2016/08/05 * Tim ...

随机推荐

  1. 获取当前请求的URL的地址、参数、参数值、各种属性

    //URL: http://localhost:1897/User/Press/UserContent.aspx/9878?id=1#toc Request.ApplicationPath; //结果 ...

  2. 引入CSS文件的@import与link的权重分析

    我很少在CSS用到@import这个标签,最近看到一句话“link方式的样式的权重 高于@import的权重”,感觉不太对,@import只是一个引入外部文件而已,怎么会有高于link的权重呢?于是我 ...

  3. CSS伪类与CSS伪元素的区别及由来

    关于两者的区别,其实是很古老的问题.但是时至今日,由于各种网络误传以及一些不负责任的书籍误笔,仍然有相当多的人将伪类与伪元素混为一谈,甚至不乏很多CSS老手.早些年刚入行的时候,我自己也被深深误导,因 ...

  4. CSS实现水平垂直同时居中的5种思路

    × 目录 [1]水平对齐+行高 [2]水平+垂直对齐 [3]margin+垂直对齐[4]absolute[5]flex 前面的话 水平居中和垂直居中已经单独介绍过,本文将介绍水平垂直同时居中的5种思路 ...

  5. java中父类与子类, 不同的两个类中的因为构造函数由于递归调用导致栈溢出问题

    /* 对于类中对成员变量的初始化和代码块中的代码全部都挪到了构造函数中, 并且是按照java源文件的初始化顺序依次对成员变量进行初始化的,而原构造函数中的代码则移到了构造函数的最后执行 */ impo ...

  6. Javaweb -- ServletContextListener

    当启动web应用后端服务时,有时需要预先从数据库或者配置文件等读取信息来配置一些全局变量之类的 这时可以用ServletContextListener,在启动服务时,加载设置基本配置 实现如下: (1 ...

  7. backbone库学习-View

    Backbone中的视图提供了一组处理DOM事件.和渲染模型(或集合)数据方法(在使用视图之前,你必须先导入jQuery或Zepto) 视图类提供的方法非常简单,我们一般在backbone.View的 ...

  8. spring aop源码实现分析

    1. 先分析Advice before执行Cglib2AopProxy的intercept方法: /** * General purpose AOP callback. Used when the t ...

  9. selenium-webdriver(python) (十六) --unittest 框架

    学习unittest 很好的一个切入点就是从selenium IDE 录制导出脚本.相信不少新手学习selenium 也是从IED 开始的. IDE学习参考: 菜鸟学自动化测试(一)----selen ...

  10. redis学习之三配置文件redis.conf 的含义

    摘自http://www.runoob.com/redis/redis-conf.html 安装redis之后的第一件事,我就开始配置密码,结果总是不生效,而我居然还没想到原因.今天突然用命令行设置了 ...