gpt4 book ai didi

thinkPHP简单实现多个子查询语句的方法

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章thinkPHP简单实现多个子查询语句的方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

本文实例讲述了thinkPHP简单实现多个子查询语句的方法。分享给大家供大家参考,具体如下:

sql语句博大精深 。

理解好sql语句,就能用好thinkphp等框架中的数据库操作 。

原sql:

?
1
2
3
4
5
6
7
8
SELECT a.*,b.* from ( SELECT a.id as opener_id,a. name , sum (c.money) as bonus_money,c. year ,c. month from sh_opener a
LEFT JOIN sh_opener_bonus b on a.id = b.opener_id
LEFT JOIN sh_incentive c on b.incentive_id = c.id
where a.agent_id = 3 and a.status = 1 and c. year = 2015 and c. month = 11
GROUP BY a.id,c. year ,c. month ) a
LEFT JOIN ( SELECT a.id as payment_id,a.opener_id,a.money as payment_money,a.trode_number from sh_opener_bonus_payment a
where a. year = 2015 and a.` month ` = 11 and a.agent_id = 3) b
on a.opener_id = b.opener_id;

这里面有两个子查询语句,其实子查询语句也是表,只不过是存在内存中罢了.

thinkphp实现:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$useYear = date ( 'Y' , strtotime ( 'last month' ));
$this ->assign( 'useYear' , $useYear );
$useMonth = date ( 'm' , strtotime ( 'last month' ));
$this ->assign( 'useMonth' , $useMonth );
// 获取上一月人员的奖金金额
// 子查询1
$whereSub1 [ 'a.agent_id' ] = $this ->agent_id;
$whereSub1 [ 'a.status' ] = 1;
$whereSub1 [ 'c.year' ] = $useYear ;
$whereSub1 [ 'c.month' ] = $useMonth ;
$subQuery1 = M()->table( 'sh_opener a' )->join( 'sh_opener_bonus b on a.id = b.opener_id' )->join( 'sh_incentive c on b.incentive_id = c.id' )->where( $whereSub1 )->group( 'a.id,c.year,c.month' )->field( 'a.id,a.name,sum(c.money) as bonus_money,c.year,c.month' )->select(false);
// 子查询2
$whereSub2 [ 'a.agent_id' ] = $this ->agent_id;
$whereSub2 [ 'a.year' ] = $useYear ;
$whereSub2 [ 'a.month' ] = $useMonth ;
$subQuery2 = M()->table( 'sh_opener_bonus_payment a' )->where( $whereSub2 )->field( 'a.id as payment_id,a.opener_id,a.money as payment_money,a.trode_number' )->select(false);
$list = M()->table( $subQuery1 . ' a' )->join( $subQuery2 . ' b on a.id = b.opener_id' )->select();
$this ->assign( 'list' , $list );

其实thinkphp框架对sql的封装,最终还是要拼凑成sql语句.

希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助.

最后此篇关于thinkPHP简单实现多个子查询语句的方法的文章就讲到这里了,如果你想了解更多关于thinkPHP简单实现多个子查询语句的方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com