[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 ...
随机推荐
- Atitit 面向对象 封装的实现原理
Atitit 面向对象 封装的实现原理 1.1. 动态对象的模拟使用map+函数接口可以实现1 1.2. 在用结构体 + 函数指针 模拟 对象 1 1.3. This指针..1 1.4. " ...
- 客户端向服务端传送特殊字符解决方法(检测到有潜在危险的 Request.Form 值)
当客户端向服务端传输特殊字符时报错,错误信息如下图:
- IDE:Eclipse查看Servlet源码
一.源码下载 1.打开tomcat官网:http://tomcat.apache.org/ , 点击右侧下载菜单(以tomcat-7为例)进入下载页面:http://tomcat.apache. ...
- 《PHP Manual》阅读笔记1
1.phpinfo() 从 PHP 获取系统信息. 2.$_SERVER 只是 PHP 自动全局化的变量之一.它包含了 web 服务器提供的所有信息,被称为超全局变量. 3.htmlspecialch ...
- linker command failed with exit code 1 (use -v to see invocation)
背景:用U盘从另一台电脑考过来后,出现错误 linker command failed with exit code 1 (use -v to see invocation) 出现这种情况很可能是,项 ...
- iOS苹果企业证书被撤销以及启用与管理
在国内, 积分墙以及各大助手(爱思助手, 91苹果助手, XY苹果助手, PP助手, 快用助手)等业务领域都在使用,苹果对证书的使用越来越严格.简单的分析一下,证书被封的原因. 一般证书被封会收到 ...
- CSS拉伸resize
CSS3新增了resize属性,该属性允许用户通过拖动的方式来修改元素的尺寸.本来resize应该翻译为缩放,但在实际测试中通过resize属性只可以在宽高基础上实现拉伸效果,而无法实现缩放到小于宽高 ...
- javase基础复习攻略《八》
进入第八篇,我们开始讨论JAVA的IO初步.在JAVA程序中,对数据的输入\输出操作以"流"(stream)方式进行,J2SDK提供了各种各样的"流"类,用于获 ...
- poj1703Find them, Catch them(并查集以及路径压缩)
/* 题目大意:有两个不同的黑帮,开始的时候不清楚每个人是属于哪个的! 执行两个操作 A a, b回答a, b两个人是否在同一帮派,或者不确定 D a, b表示a, b两个人不在同一个帮派 思路:利用 ...
- Servlet的四种映射模式
1. 路径匹配模式 <url-pattern>/test/*</url-pattern> 2. 扩展名匹配模式 <url-pattern>*.do</url- ...