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 构建子查询的更多相关文章

  1. ThinkPHP 数据库操作(七) : 视图查询、子查询、原生查询

    视图查询 视图查询可以实现不依赖数据库视图的多表查询,并不需要数据库支持视图,例如: Db::view('User','id,name') ->view('Profile','truename, ...

  2. Laravel Query Builder 复杂查询案例:子查询实现分区查询 partition by

    案例 案例:Laravel 在文章列表中附带上前10条评论?,在获取文章列表时同时把每个文章的前10条评论一同查询出来. 这是典型分区查询案例,需要根据 comments 表中的 post_id 字段 ...

  3. thinkphp 子查询

    从3.0版本开始新增了子查询支持,有两种使用方式: 大理石平台检验标准 1.使用select方法 当select方法的参数为false的时候,表示不进行查询只是返回构建SQL,例如: // 首先构造子 ...

  4. 子查询语句的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 ...

  5. Sql Server系列:子查询

    1 子查询概念 子查询是嵌套在另一个查询中的普通T-SQL查询.在有一个SELECT语句通过使用小括号创建子查询,作为另一个查询的部分数据或条件的基础. 子查询通常用于满足以下某个需求: ◊ 将一个查 ...

  6. LINQ之路 7:子查询、创建策略和数据转换

    在前面的系列中,我们已经讨论了LINQ简单查询的大部分特性,了解了LINQ的支持计术和语法形式.至此,我们应该可以创建出大部分相对简单的LINQ查询.在本篇中,除了对前面的知识做个简单的总结,还会介绍 ...

  7. ThinkPHP(3)SQL查询语句

    ThinkPHP中对查询语句,包含了基本的查询方式.表达方式.快速查询.区间查询.组合查询.SQL查询.动态查询和子查询. 一.查询方式 ThinkPHP提供了三种基本的查询方式:字符串条件查询.索引 ...

  8. Oracle子查询(嵌套查询)

    概念: 所谓子查询,即一个select语句中嵌套了另外的一个或者多个select语句 需求:查找和Smith同部门的所有员工的id和last_name 目标: 员工id,last_name from: ...

  9. thinkphp where()条件查询

    今天来给大家讲下查询最常用但也是最复杂的where方法,where方法也属于模型类的连贯操作方法之一,主要用于查询和操作条件的设置.where方法的用法是ThinkPHP查询语言的精髓,也是Think ...

随机推荐

  1. Ubuntu14.04下安装Libsvm,并使用Libsvm

    (1)Ubuntu14.04下安装Libsvm 转载:https://blog.csdn.net/katrinawj/article/details/78915874 一.下载: 网址:http:// ...

  2. 算法-Java组合

    code: import org.assertj.core.util.Lists; import java.util.ArrayList; import java.util.Collections; ...

  3. Asp.net中文本框全选的实现

    一.鼠标滑过textbox全选 前台: <asp:TextBox runat="server" onMouseOver="this.focus();this.sel ...

  4. php 区分0和空

    能够区分出来的有2,4,6 方法 public function test(){ $test=; if($test==''){ echo '<br />在php中1,0即为空'; //被输 ...

  5. 【PCA】

    http://blog.csdn.net/xiaojidan2011/article/details/11595869 非常清楚 核心部分解释:主成份用于降纬,通过线型变换,从高纬度映射到低纬度,其中 ...

  6. 7 Django系列之关于bootstrap-table插件的简单使用

    preface 我们在前端html页面的时候做表格最常用的就是jinja2渲染,这样的好处是无须引用外部插件,直接使用就行,方便.但是不好的就是前端CSS样式以及其他表格排序筛选功能需要自己写,增加了 ...

  7. 复习js

    js写在页面最后如果放在前面,需要加window.onload=function(){)常见的两种输出方式 在网页中弹出显示框,显示信息 alert() 在控制台输出消息,一般用来调试程序consol ...

  8. phoenix连接hbase数据库,创建二级索引报错:Error: org.apache.phoenix.exception.PhoenixIOException: Failed after attempts=36, exceptions: Tue Mar 06 10:32:02 CST 2018, null, java.net.SocketTimeoutException: callTimeou

    v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...

  9. 用python开发android应用 【转载】

    用python开发android应用 [转载] 转载自:http://www.miui.com/thread-995114-1-1.html Python是动态语言,比较简洁.Android不直接支持 ...

  10. photoshop制作简单ico图标

    新建16 * 16透明画布 字体20px 半径4px