[moka同学笔记]Yii2 数据操作Query Builder
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
- 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();
- 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) {
- // 一行一行取
- }
[moka同学笔记]Yii2 数据操作Query Builder的更多相关文章
- [moka同学笔记]Yii2 数据操作Query Builder 2
Query Builder $rows = (new \yii\db\Query()) ->select(['dyn_id', 'dyn_name']) ->from('zs_dynast ...
- Yii2 数据操作Query Builder
转载地址: http://blog.csdn.net/hzqghost/article/details/44117081 Yii2 数据操作Query Builder 分类: Yii22015-03- ...
- Yii2 数据操作Query Builder(转)
Query Builder $rows = (new \yii\db\Query()) ->select(['dyn_id', 'dyn_name']) ->from('zs_dynast ...
- Yii2 数据操作Query Builder查询数据
Query Builder $rows = (new \yii\db\Query()) ->select(['dyn_id', 'dyn_name']) ->from('zs_dynast ...
- [moka同学笔记]yii2.0查询数据库
一. [:id占位符]使用 $results = Test::findBySql($sql,array(':id'=>'1 or 1=1))->all() 二. [id=1] 选 ...
- [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 ...
- [moka同学笔记]yii2.0缓存
1.控制器中CacheDemoController.php <?php /** * Created by PhpStorm. * User: moka同学 * Date: 2016/06/29 ...
- [moka同学笔记]Yii2.0 modal的使用
第一次使用,时候不明白什么原理,大概用了几次后,才模模糊糊搞清楚原来是怎么一回事,现在就把写过的代码,贴在下边. 1.在视图文件中, 第一步首先在index.php文件中 做了一个a链接的按钮 调用了 ...
- [moka同学笔记]yii2.0表单的使用
1.创建model /biaodan.php <?php /** * Created by PhpStorm. * User: moka同学 * Date: 2016/08/05 * Tim ...
随机推荐
- WinMTR
一般在windows 来判断网络连通性用ping 和tracert,ping的话可以来判断丢包率,tracert可以用来跟踪路由. 在Linux中有一个好用的网络连通性判断工具,它可以结合ping n ...
- JS操作页面
DOM操作 1 windows对象操作 属性(值或者子对象): opener:打开当前窗口的源窗口,如果当前窗口是首次启动浏览器打开的,则opener是null.可以利用这属性来关闭源窗口 方法(函数 ...
- HQL查询语言——转载(http://www.cnblogs.com/20gg-com/p/6045739.html)
Hibernate查询语言(HQL)是一种面向对象的查询语言,类似于SQL,但不是对表和列操作,HQL适用于持久对象和它们的属性. HQL查询由Hibernate转换成传统的SQL查询,这在圈上的数据 ...
- Android 坐标系和 MotionEvent 分析、滑动
1.Android坐标系 在Android中,屏幕最左上角的顶点作为Android坐标系的原点,这个点向左是X轴正方向,这个点向下是Y轴正方向. 系统提供了getLocationOnScreen(in ...
- CSS层模型
参考:慕课网 点此可进 如何让html元素在网页中精确定位,就像图像软件PhotoShop中的图层一样可以对每个图层能够精确定位操作.CSS定义了一组定位(positioning)属性来支持层布局模型 ...
- iBatis + SQL Server 项目开发实战小结
几年前跟随项目经理做的一个ERP小项目,自己业余时间整理的开发手册,供参考. 开发环境配置:编程环境为Microsoft Visual Studio 2010,数据库是SQL Server 2008 ...
- WPF自定义控件与样式(5)-Calendar/DatePicker日期控件自定义样式及扩展
一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: 日历控 ...
- vs xamarin android StartActivity
跳转无需intent,直接给要跳转的activity的type就可以了 StartActivity(typeof(Login));
- Linux一块网卡添加多个IP地址
环境: RHEL6.4 需求: Linux一块网卡添加多个IP地址 一.临时生效 1.1 网卡eth0添加一个IP地址 1.2 修改eth0:0的广播地址 二.永久生效 2.1 编辑ifcfg-eth ...
- Linux - 变量
printenv - print all or part of environment 显示所有变量:print 显示某个变量:print <variable name> 或者 echo ...