thinkphp 构建子查询
thinkphp构建子查询sql语句写法
从3.0版本开始新增了子查询支持,有两种使用方式:
1、使用select方法 当select方法的参数为false的时候,表示不进行查询只是返回构建SQL,例如:
// 首先构造子查询SQL
$subQuery = $model->field('id,name')->table('tablename')->group('field')->where($where)->order('status')->select(false);
当select方法传入false参数的时候,表示不执行当前查询,而只是生成查询SQL。
2、使用buildSql方法
$subQuery = $model->field('id,name')->table('tablename')->group('field')->where($where)->order('status')->buildSql();
调用buildSql方法后不会进行实际的查询操作,而只是生成该次查询的SQL语句(为了避免混淆,会在SQL两边加上括号),然后我们直接在后续的查询中直接调用。
// 利用子查询进行查询
$model->table($subQuery.' a')->where()->order()->select()
构造的子查询SQL可用于ThinkPHP的连贯操作方法,例如table where等。
例子:
$subQuery = M('Withdraw')
->alias('a')
->field('transaction_id,(select username from ' . C("DB_PREFIX") . 'user as b where a.user_id = b.user_id) as username,(select true_name from ' . C("DB_PREFIX") . 'user as b where a.user_id = b.user_id) as true_name,amount,withdraw_id,bank_name,bank_subbranch_name,card_number,apply_time,withdraw_status,withdraw_time,handle_mark')
->union('SELECT `transaction_id`,
(select username from '.C("DB_PREFIX").'shop as d left join '.C("DB_PREFIX").'user as b on d.principal_id=b.user_id where a.shop_id = d.shop_id) as username,
(select shop_name from '.C("DB_PREFIX").'shop as b where a.shop_id = b.shop_id) as true_name,
`amount`,`withdraw_id`,`bank_name`,`bank_subbranch_name`,`card_number`,`apply_time`,`withdraw_status`,`withdraw_time`,`handle_mark`
FROM 1dcq_shop_withdraw a')->buildSql();
$table = M()->field('transaction_id,username,true_name,amount,withdraw_id,bank_name,bank_subbranch_name,card_number,apply_time,withdraw_status,withdraw_time,handle_mark')
->table($subQuery.' n')
->where($where)
->order('apply_time desc')
->select();
echo M()->getLastSql();exit;
输出:SELECT `transaction_id`,`username`,`true_name`,`amount`,`withdraw_id`,`bank_name`,`bank_subbranch_name`,`card_number`,`apply_time`,`withdraw_status`,`withdraw_time`,`handle_mark` FROM ( SELECT `transaction_id`,(select username from 1dcq_user as b where a.user_id = b.user_id) as username,(select true_name from 1dcq_user as b where a.user_id = b.user_id) as true_name,`amount`,`withdraw_id`,`bank_name`,`bank_subbranch_name`,`card_number`,`apply_time`,`withdraw_status`,`withdraw_time`,`handle_mark` FROM 1dcq_withdraw a UNION SELECT `transaction_id`,(select username from 1dcq_shop as d left join 1dcq_user as b on d.principal_id=b.user_id where a.shop_id = d.shop_id) as username,(select shop_name from 1dcq_shop as b where a.shop_id = b.shop_id) as true_name,`amount`,`withdraw_id`,`bank_name`,`bank_subbranch_name`,`card_number`,`apply_time`,`withdraw_status`,`withdraw_time`,`handle_mark` FROM 1dcq_shop_withdraw a ) n WHERE `withdraw_status` NOT IN ('0','1','3') ORDER BY apply_time desc
thinkphp 构建子查询的更多相关文章
- ThinkPHP 数据库操作(七) : 视图查询、子查询、原生查询
视图查询 视图查询可以实现不依赖数据库视图的多表查询,并不需要数据库支持视图,例如: Db::view('User','id,name') ->view('Profile','truename, ...
- Laravel Query Builder 复杂查询案例:子查询实现分区查询 partition by
案例 案例:Laravel 在文章列表中附带上前10条评论?,在获取文章列表时同时把每个文章的前10条评论一同查询出来. 这是典型分区查询案例,需要根据 comments 表中的 post_id 字段 ...
- thinkphp 子查询
从3.0版本开始新增了子查询支持,有两种使用方式: 大理石平台检验标准 1.使用select方法 当select方法的参数为false的时候,表示不进行查询只是返回构建SQL,例如: // 首先构造子 ...
- 子查询语句的thinkphp实现
语句 SELECT a.id as item_id,a.name as item_name,a.intro as item_intro,b.id,b.money FROM sh_incentive_i ...
- Sql Server系列:子查询
1 子查询概念 子查询是嵌套在另一个查询中的普通T-SQL查询.在有一个SELECT语句通过使用小括号创建子查询,作为另一个查询的部分数据或条件的基础. 子查询通常用于满足以下某个需求: ◊ 将一个查 ...
- LINQ之路 7:子查询、创建策略和数据转换
在前面的系列中,我们已经讨论了LINQ简单查询的大部分特性,了解了LINQ的支持计术和语法形式.至此,我们应该可以创建出大部分相对简单的LINQ查询.在本篇中,除了对前面的知识做个简单的总结,还会介绍 ...
- ThinkPHP(3)SQL查询语句
ThinkPHP中对查询语句,包含了基本的查询方式.表达方式.快速查询.区间查询.组合查询.SQL查询.动态查询和子查询. 一.查询方式 ThinkPHP提供了三种基本的查询方式:字符串条件查询.索引 ...
- Oracle子查询(嵌套查询)
概念: 所谓子查询,即一个select语句中嵌套了另外的一个或者多个select语句 需求:查找和Smith同部门的所有员工的id和last_name 目标: 员工id,last_name from: ...
- thinkphp where()条件查询
今天来给大家讲下查询最常用但也是最复杂的where方法,where方法也属于模型类的连贯操作方法之一,主要用于查询和操作条件的设置.where方法的用法是ThinkPHP查询语言的精髓,也是Think ...
随机推荐
- 内存与cpu的关系
CPU是负责运算和处理的,内存是交换数据的.当程序或者操作者对CPU发出指令,这些指令和数据暂存在内存里,在CPU空闲时传送给CPU,CPU处理后把结果输出到输出设备上,输出设备就是显示器,打印机等. ...
- mysql使用sql语句查询数据库所有表注释已经表字段注释
场景: 1. 要查询数据库 "mammothcode" 下所有表名以及表注释 /* 查询数据库 ‘mammothcode’ 所有表注释 */ SELECT TABLE_NAME,T ...
- SAX解析XML笔记
关于基本操作,请参考:Java用SAX解析XML,这里不重复造轮子了,以下是个人笔记:
- slab着色,可以减少cache conflict miss概率么?
以内部slab为例,管理区 + object总大小+left_over size = 1page,我们做个极端假设,cache为 direct-mapped caches. 1.没有采用slab着色: ...
- Linux定时运行程序脚本
项目开发中经常需要用将某个脚本定时运行从而避免其运行并不会影响到其他程序.虽说可以在脚本中利用sleep命令来做这一点,Linux自带了crontab命令可以很好的执行这一操作. 比如我们有名为som ...
- linq to xml 初学 -- 查询语法
初学linq to xml,很方便 string path = ch.GetConfigFile("")+ "SpeedDial.xml"; var sdDo ...
- Simply Syntax(思维)
Simply Syntax Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5551 Accepted: 2481 Des ...
- lua 注释
1. 单行注释 -- 功能等同于C++中的// 2. 多行注释 --[[ 注释的内容 ]] 功能等同于C++中的 /**/ 3. 多行注释 --[====[ 注释和内容 ]=== ...
- Webgoat学习笔记
Webgoat 分为简单版和开发版,具体版本及安装方法详见:https://github.com/WebGoat/WebGoat 本机环境为:Windows+Tomcat,故下载war包,自动解压缩进 ...
- VisualSVN破解
先讲下破解原理 首先,去VisualSVN官网下载最新版本. 传送门:http://www.visualsvn.com/server/download/ 定位到VisualSVN安装目录,C:\Pro ...