Yii2 数据操作Query Builder(转)
Query Builder
$rows = (new \yii\db\Query())
->select(['dyn_id', 'dyn_name'])
->from('zs_dynasty')
->where(['between','dyn_id', ,])
->limit()
->all();
print_r($rows);
use yii\db\Query;
$query = (new Query())
->from('user')
->orderBy('id');
$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()->
$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('status=1')->
where('status=:status', [':status' => $status])->
where([
'status' => ,
'type' => null,
'id' => [, , ],
])->
-------
$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', , ] //id BETWEEN 1 AND 10
['not between', 'id', , ] //not id BETWEEN 1 AND 10
['in', 'id', [, , ]] //id IN (1, 2, 3)
['not in', 'id', [, , ]] //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', ] //age>10
$status = ;
$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,
]);
Group by
$query->groupBy('id, status');
$query->addGroupBy(['created_at', 'updated_at']);
$query->having(['status' => $status]);
//having,andHaving,orHaving
$query->limit(10);
$query->offset(10);
- 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 为字段或表达式
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) {
}
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) {
// 一行一行取
}
Yii2 数据操作Query Builder(转)的更多相关文章
- Yii2 数据操作Query Builder
转载地址: http://blog.csdn.net/hzqghost/article/details/44117081 Yii2 数据操作Query Builder 分类: Yii22015-03- ...
- [moka同学笔记]Yii2 数据操作Query Builder 2
Query Builder $rows = (new \yii\db\Query()) ->select(['dyn_id', 'dyn_name']) ->from('zs_dynast ...
- [moka同学笔记]Yii2 数据操作Query Builder
Query Builder [php] view plain copy $rows = (new \yii\db\Query()) ->select(['dyn_id', 'dyn_name ...
- Yii2 数据操作Query Builder查询数据
Query Builder $rows = (new \yii\db\Query()) ->select(['dyn_id', 'dyn_name']) ->from('zs_dynast ...
- Yii2 数据操作DAO
参考: http://www.yiiframework.com/doc/guide/1.1/zh_cn/database.dao http://blog.csdn.net/hzqghost/artic ...
- Yii2 三层设计模式:SQL Command、Query builder、Active Record(ORM)
用Yii2也有一段时间了,发现Yii2 Framework对Database的操作有非常良好的结构和弹性. 接下来介绍三种数据库操作方式. SQL Command Level: // Get DB c ...
- Studio 3T 如何使用 Query Builder 查询数据
Studio 3T 是一款对 MongoDB 进行数据操作的可视化工具. 在 Studio 3T 中,我们可以借助 Query Builder 的 Drag & Drop 来构建查询条件. 具 ...
- Yii的学习(3)--查询生成器 (Query Builder)
原文地址:http://www.yiiframework.com/doc/guide/1.1/en/database.query-builder 不过原文是英文的,Yii的官网没有翻译这一章,自己就尝 ...
- Yii Query Builder insert()、update()、delete()使用
Yii自带的query builder还是很好用的,省去了拼sql的过程,今天在写一个语句的时候遇到这样一个问题 $connection = Yii::app()->db; $command = ...
随机推荐
- jquery失去焦点与获取焦点事件blur() focus()
以前我们在js中写input各种事件时都会直接在input中写,昨天开始我开始全面使用jquery了,现在来谈一下我对jquery blur() focus()事件的学习笔记. 对于元素的焦点事件,我 ...
- [cf621E]Wet Shark and Blocks
Description 给定$n$个数和$b$个盒子,放一些数到盒子中,使得盒子不为空.每个盒子中的数是一样的,一个数可以被放到多个盒子中. 从每个盒子中取一个数,组成一个$b$位数,如果这个数$mo ...
- 【BZOJ-2400】Spoj839Optimal Marks 最小割 + DFS
2400: Spoj 839 Optimal Marks Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 567 Solved: 202[Submit ...
- 【BZOJ-3196】二逼平衡树 线段树 + Splay (线段树套平衡树)
3196: Tyvj 1730 二逼平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2271 Solved: 935[Submit][Stat ...
- Mono开发环境搭建(Windows)
一.下载 1.登录http://www.mono-project.com/下载 2.开发工具Xamarin Studio(好像是免费) http://www.monodevelop.com/downl ...
- 添加一个功能Action
1,只用一个handler类,所有都事件的处理器都在一个handler类 handler要创建以Action为名称的方法 event要单独分开,继承KDEvent package com.kingde ...
- python模块
模块介绍 为什么用模块 在代码量庞大的时候,如果靠一个脚本来进行函数式编程,对于脚本的可读性及后期脚本的维护带来极大的不便,而且无法分辨函数间的调用关系,所以,可以将一组功能类似的函数单独放在一个py ...
- 远程连接RabbitMQ失败
远程连接RabbitMQ失败 为了避免污染宿主系统环境,于是在虚拟机中搭建了一个linux环境并且按照了rabbitmq-server.然后在远程连接的时候一直连接失败. 官网上面给的例子都是在本地使 ...
- hdu 2031 杨辉三角
题意:显示杨辉三角形. 解法: 组合数学公式:combi(n,m)=combi(n-1,m-1)+combi(n-1,m); 至于为什么有这个公式呢?那就是高中数学的内容啦 1: #include&l ...
- hdu 1863 - 畅通工程(MST)
畅通工程 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...