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 = ...
随机推荐
- Mac OS X 上的Apache配置
Mac系统自带apache服务器 查看apache版本 sudo apachectl -v 启动apache sudo apachectl start 重启apache sudo apachectl ...
- [usaco dec 15] 卡牌游戏 cardgame [贪心]
题面: 传送门 思路: 这道题官方标准解法是线段树维护一堆奇奇怪怪的东西......我用的是贪心 方法很简单,处理出pre和suf数组,分别代表前i张.后i张牌在最优方案下打出时可以得到的分数,然后两 ...
- linux删除大量文件
1.建立一个空目录 mkdir -p /tmp/rsync_blank 2.确立需要清空的目标目录 /data/web/vip/htdocs/tuan 3.使用rsync同步删除(注意目录后面的“/” ...
- mysql 各种级联查询后更新(update select).
mysql 各种级联查询后更新(update select). CREATE TABLE `tb1` ( `id` int(11) NOT NULL, `A` varchar(100) de ...
- pat 团体天梯赛 L2-011. 玩转二叉树
L2-011. 玩转二叉树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜 ...
- 【HDOJ5559】Frog and String(构造)
题意:给定n,m,k,要求构造出一个长度为n,最多使用前k个大写字母,有m个不同回文子串的字符串 1<=n,m<=1e5,1<=k<=26 思路:打表找规律 本质上是要找到不让 ...
- 8个学习.net的博客链接 (以前收藏过更多的,被百度新版搞没了,恨死了)
原文发布时间为:2012-09-18 -- 来源于本人的百度文章 [由搬家工具导入] Simone Chiaretta’s CodeClimber http://www.haacked.com/ ( ...
- .NET结束外部进程 C#结束外部进程
原文发布时间为:2011-02-15 -- 来源于本人的百度文章 [由搬家工具导入] using System;namespace ConsoleApplication2{ class Prog ...
- ASP.NET MVC 1.0 哈哈。。转过来,还没学。。
原文发布时间为:2009-06-13 -- 来源于本人的百度文章 [由搬家工具导入] ASP.NET MVC 1.0 发布了投递人 itnews 发布于 2009-03-19 00:46 评论(8) ...
- i2c 协议解析【转】
转自:http://blog.csdn.net/g_salamander/article/details/8016698 版权声明:本文为博主原创文章,未经博主允许不得转载. 1.基本概念 主机 ...