Yii2 数据操作Query Builder
转载地址:
http://blog.csdn.net/hzqghost/article/details/44117081
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
- $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();
- 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的更多相关文章
- [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 数据操作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 = ...
随机推荐
- C++指针详解 (转)
C++指针详解 指针的概念 指针是一个特殊的变量,它里面存储的数值被解释成为内存里的一个地址.要搞清一个指针需要搞清指针的四方面的内容:指针的类型,指针所指向的类型,指针的值或者叫指针所指向的内存区, ...
- LeetCode118 杨辉三角 Python
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 示例:输入: 5 输出: [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] cl ...
- 使用hibernate建立mysql连接以及生成映射类和配置文件*.cfg.xml
建立数据库连接 找到window—open perspective—myeclipse database explore空白出右键new注意 driver template 和driver class ...
- HDFS设计思想
HDFS设计思想 DataNode:用来在磁盘上存储数据 HDFS 数据存储单元( block ) 1 文件被切分成固定大小的数据block块 •默认数据块大小为 64MB(hadoop1.x版本6 ...
- javascript作用域链理解
执行上下文(Execution context,简称EC) 概念 每当控制器到达ECMAScript可执行代码的时候,就进入了一个执行上下文. javascript中,EC分为三种: ...
- python获取目录下文件夹名称
path = '/opt' dirs = os.listdir(path) for dir in dirs: print dir
- 小M的作物 最大权闭合子图
题目大意 bzoj 3438 两个田\(A,B\) \(n\le 1000\)种作物的种子 第\(i\)个种子,种\(A\)价值\(a[i]\),种\(B\)价值\(b[i]\) 再给出\(m\)个子 ...
- 星际战争(bzoj 3993)
Description 3333年,在银河系的某星球上,X军团和Y军团正在激烈地作战.在战斗的某一阶段,Y军团一共派遣了N个巨型机器人进攻X军团的阵地,其中第i个巨型机器人的装甲值为Ai.当一个巨型机 ...
- 【BZOJ4300】绝世好题(二进制,DP)
题意: n<=100000,ai<=2*10^9 思路:按二进制逐位考虑,只要有至少1位取and后为1就可以接下去 设dp[i]为第i位取and之后为1的最长的序列长度,意会一下 #inc ...
- WIN8.1安装 .net framework 3.5
1.虚拟光驱加载安装镜像 2.以管理员权限运行cmd 3.输入以下命令: dism.exe /online /enable-feature /featurename:NetFX3 /Source:X: ...