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. PHP性能优化工具–xhprof安装

    PHP性能优化工具–xhprof安装,这里我先贴出大致的步骤: 1.获取xhprof 2.编译前预处理 3.编译安装 4.配置php.ini 5.查看运行结果 那么下面我们开始安装xhprof工具吧: ...

  2. Android 神兵利器—— Git 常用命令

    总结的Android 工具类文章: Android 神兵利器-- Adb 常用命令 Android 神兵利器-- Git 常用命令 在项目研发时,经常使用Git,基本的命令有六个,通过下面的图片我们可 ...

  3. EasyUI Field

    效果: JS: var sortIndex = $("#ListDiv").find(".datagrid-view2").find(".datagr ...

  4. tool list

    http://www.pairwise.org/tools.asp 用例设计工具  组合测试工具pict 代码调试 Findbugs  介绍及使用方法 接口测试工具 webservices studi ...

  5. Enterprise Solution 界面设计规范

    Enteprise Solution有一套自己的界面设计规范,也是很多年(10年以上)管理软件界面精华的积累.没有一个软件从一开始就很善于界面设计,许多个小小的改善,比如控件位置的移动,控件摆放顺序的 ...

  6. s2sh框架搭建(辅助工具:MyEclipse)及解决一些遇到的问题

    1.新建一个web project 2.首先生成Hibernate Facet 3.Hibernate Facet 安装步骤 4.然后是spring facet安装步骤 5.最后是struts fac ...

  7. android 处理302地址

    最近项目中需要用到重定向下载,所以找了很多的方法都不合适.因为下载的链接并非单纯的地址,而是需要多次转发的, 在下载的时候用的是URL来打开数据流.但是多次测试并不能对多次跳转的链接打开请求.对于30 ...

  8. Vim多行缩进技巧

    按v进入visual状态,选择多行,用>或<缩进或缩出 通常根据语言特征使用自动缩进排版:在命令状态下对当前行用== (连按=两次), 或对多行用n==(n是自然数)表示自动缩进从当前行起 ...

  9. 百度Map与HT for Web结合的GIS网络拓扑应用

    在<HT for Web整合OpenLayers实现GIS地图应用>篇中介绍了HT for Web与OpenLayers的整合,不少朋友反应国内用得比较多的还是百度地图,虽然HT整合百度地 ...

  10. Oracle Flashback和RMAN示例

    作者:Grey 原文地址:http://www.cnblogs.com/greyzeng/p/5346833.html 环境: Windows 10 专业版 Oracle Database 12c R ...