Query Builder

    $rows = (new \yii\db\Query())
->select(['dyn_id', 'dyn_name'])
->from('zs_dynasty')
->where(['between','dyn_id', 1,30])
->limit(10)
->all();
print_r($rows);
    use yii\db\Query;
$query = (new Query())
->from('user')
->orderBy('id');
SELECT

    $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

    $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

    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

    $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

$query->filterWhere([
'username' => $username,
'email' => $email,
]);
//如果email为空,则 WHERE username=:username
ORDER BY
    $query->orderBy([
'id' => SORT_ASC,
'name' => SORT_DESC,
]);
//orderBy , addOrderBy
GROUP BY
    $query->groupBy('id, status');
$query->addGroupBy(['created_at', 'updated_at']);
HAVING
    $query->having(['status' => $status]);
//having,andHaving,orHaving
 

LIMIT OR OFFSET

    $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

    $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 \yii\db\Query())
->from('user')
->where(['last_name' => 'Smith'])
->count();
//SELECT COUNT(*) FROM `user` WHERE `last_name`=:last_name $command = (new \yii\db\Query())
->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

    use yii\db\Query;
$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

    use yii\db\Query;
$query = (new Query())
->from('user')
->orderBy('id');
foreach ($query->batch() as $users) {
// batch( $batchSize = 100, $db = null )
// 一个批次取100行
}
foreach ($query->each() as $user) {
// 一行一行取
}
 
 
 
 

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

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

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

  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. Atitit usrQBF2312 命名空间pkg 以及 api命名 spec规范

    Atitit usrQBF2312 命名空间pkg 以及 api命名 spec规范 简化英文1 常用类库sdk的命名单词统计表1 简化时间规则1 Namsspace nam spec,参照java . ...

  2. Atitti 数据库事务处理 attilax总结

    Atitti 数据库事务处理 attilax总结 1.1. 为什么要传递Connection?1 1.2. 两种事务处理方式,一种是编程式事务处理;一种是声明...2 1.3. 事务隔离级别 2 1. ...

  3. js防止客户端多触发

    代码: /***防止多触发**id 必须唯一*fn 回掉函数*wait 延迟多长时间**使用例子:* ToPreventMoreTrigger('id', function () {//注意 id 是 ...

  4. js在控件原有的事件方法中加入自己的方法

    有没有碰到过这样的情况,在一个别人的页面上,你想为某个按钮加入自己的控制逻辑,满足条件的情况下才执行原有的事件方法呢? 这个时候在不能修改其原有方法的情况下,先获取控件的事件方法,并将其包装到自己的控 ...

  5. SQL优化快速入门

    最近遇到一个专门进行SQL技术优化的项目,对很多既有的老存储过程进行调优(现在已经不再新增任何存储过程),因此系统的对SQL语句编写进行一次科学的学习变得很有必要.这儿将基于黄德承大神的Oracle ...

  6. Lookup 组件异常

    Lookup组件有两个数据源,一个是上流组件的输出,一个是组件lookup的数据源,这个数据源是在Connection选项卡中进行配置.在开发package的过程中,我发现一个异常,当Lookup数据 ...

  7. Android自定义Dialog及其布局

     实际项目开发中默认的Dialog样式无法满足需求,需要自定义Dialog及其布局,并响应布局中控件的事件. 上效果图: 自定义Dialog,LogoutDialog: 要将自定义布局传入构造函数中, ...

  8. 算法设计和数据结构学习_5(BST&AVL&红黑树简单介绍)

    前言: 节主要是给出BST,AVL和红黑树的C++代码,方便自己以后的查阅,其代码依旧是data structures and algorithm analysis in c++ (second ed ...

  9. nodejs 生产环境配置

    1.node 默认为development(开发)模式 启动node 时可以设置模式为生产模式 set NODE_ENV=production node app.js 2.设置node监听的端口 va ...

  10. linux专题三之如何悄悄破解root密码(以redhat7.2x64为例)

    root用户在linux系统中拥有至高无上的权限.掌握了root密码,差不对可以对linux系统随心所欲了,当然了,root用户也不是权限最高的用户. 但是掌握了root密码,基本上够我们用了.本文将 ...