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. KnockoutJS 3.X API 第四章 表单绑定(12) selectedOptions、uniqueName绑定

    selectedOptions绑定目的 selectedOptions绑定控制当前选择多选列表中的哪些元素. 这旨在与<select>元素和选项绑定结合使用. 当用户选择或取消选择多选列表 ...

  2. 为什么MVC不是一种设计模式

    http://damoqiongqiu.iteye.com/blog/1949256 ---比较Backbone和Ext4.x在MVC实现上的差异 大漠穷秋 前言 圣人云:不想做妈咪的小姐不是好码农. ...

  3. Android入门(七)碎片的生命周期与限定符

    原文链接:http://www.orlion.ga/560/ 这篇文章实际已经在上篇文章中写的差不多了,但是万恶的wordpress没保存!已经不止一次出现这种情况了! 一.碎片的生命周期 1.碎片的 ...

  4. 深入理解CSS伪类

    × 目录 [1]锚点 [2]UI元素 [3]结构伪类[4]其他 前面的话 伪类经常与伪元素混淆,伪元素的效果类似于通过添加一个实际的元素才能达到,而伪类的效果类似于通过添加一个实际的类来达到.实际上c ...

  5. java接口的应用举例

    /* 接口的理解: 接口就是前期定义一个规则!某一个类A,为了扩展自身的功能,对外提供这个接口,后期只要是符合这个接口(规则) 的类(这个类是接口的子类),将子类对象的引用传递给类A中方法(方法中的参 ...

  6. javaweb -- 获取请求IP(附实现源码)

    实现步骤如下: (1)新建一个java web工程 (2)新建servers包,新建类GetRequestIp,用来处理获取请求IP,GetRequestIp类完整代码如下: package serv ...

  7. php基础教程-输出Hello World

    <!DOCTYPE html> <!--!文档类型,一个文档类型标记是一种标准通用标记语言的文档类型声明, 它的目的是要告诉标准通用标记语言解析器,它应该使用什么样的文档类型定义(D ...

  8. Tomcat7.0启动报错:java.lang.illegalargumentexception:taglib definition not consisten with specification version

    Tomcat7.0启动报错:java.lang.illegalargumentexception:taglib definition not consisten with specification ...

  9. android用讯飞实现TTS语音合成 实现中文版

    Android系统从1.6版本开始就支持TTS(Text-To-Speech),即语音合成.但是android系统默认的TTS引擎:Pic TTS不支持中文.所以我们得安装自己的TTS引擎和语音包. ...

  10. ArrayBuffer简析

    关键技术: JavaScript,ArrayBuffer,Type Array,DataView,Web Worker,性能对比 ArrayBuffer 在文章开头列出了这些关键字,主要就是让大家了解 ...