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. Java泛型概述

    泛型是Java中一个非常重要的知识点,在Java集合类框架中泛型被广泛应用.本文我们将从零开始来看一下Java泛型的设计,将会涉及到通配符处理,以及让人苦恼的类型擦除. 泛型基础 泛型类 我们首先定义 ...

  2. PHP和Apache结合 Apache默认虚拟主机

  3. 史上最全的 Sublime Text 汉化、插件安装合集

    0.前言 本文用于给新手小白用户指明前进方向.不用做商业推广. 其次,鼓舞购买正版.拒绝盗版. 好了.口号喊完,接下来就直接開始正文. 1. Sublime Text 介绍 首先在開始前,先来介绍一下 ...

  4. 安装配置和使用HBASE Cluster(基于发行版CDH5.0.2)——系列随笔

    本系列文章只是记录了笔者本人在学习实验安装和使用基于CDH5.0.2的HBASE集群过程中的一些经验教训和心得,绝不是详细的安装过程,因本人不过一初学者,很多方面不甚了了,如果能让不幸读到的人有所得则 ...

  5. POI简易帮助文档系列--读取Excel文件

    上篇博客通过简单的几行代码就学会了POI新建Excel文档的使用,本篇博客也从简单出发,通过查看POI的官网文档和一个简单的代码实例,学习怎么遍历出一个Excel文档的内容. package com. ...

  6. QT编译错误:invalid application of 'sizeof' to incomplete type 'Qt3DRender::QPickEvent'

    执行3D常将中实体的pick操作,结果出现了编译错误:invalid application of 'sizeof' to incomplete type 'Qt3DRender::QPickEven ...

  7. v9定时发布的简单实现方法[支持静态生成]

    将以下代码放到 api/count.php 文件最后 的 ?>之前 //add 定时发布审核功能 $modelid = $modelid ? $modelid : intval($_GET['m ...

  8. C 语言与动态库相关基础知识

    1.导入文件<>和“”的区别 #include <xxx.h>导入的是标准目录下的.h文件,所谓的标准目录指的是:/use/local/include(一般是第三方头文件)以及 ...

  9. java TreeNode接口

    今天写安卓程序见到一个方法getChildAt();不懂其用边去百度搜索了一下,看到了它的api,细致看看原来是在接口里面如今我把这个api贴给大家共享假设是操作xml我认为用这个非常方便 javax ...

  10. Windows系统下运行某些程序时缺少“Msflxgrd.ocx”的解决方法

    出现这样的错误就是系统缺少相应的库文件,我们安装即可. 下载Msflxgrd.ocx,这里提供一个下载网址:https://www.ocxme.com/files/msflxgrd_ocx 64位系统 ...