thinkphp 3.2 多表查询 group

本文分析了使用PHP框架进行复杂SQL查询的方法,包括多表连接、字段筛选、分组及排序等高级技巧,并提供了多个示例对比不同的写法及其效果。

分析一

$res = M('member')
                    ->table('__MEMBER__ as a')
                    ->join('__ORDER__ as b')
                    ->field('a.id,b.order_sn,count(b.id) as total')
                    ->where('b.receive_member_id = a.id')
                    ->group('a.id')
                    ->order('total desc')
                    ->limit($page->firstRow, $page->listRows)
                    ->select();

1. $res = M('member') ,$res = M('member a') ,$res = M('member as a') 意思相同

2.$res = M('member')->table('__MEMBER a')  , $res = M('member')->table('__MEMBER as a') 相同

 

分析二

$res = M('member_attest')
                ->table('__MEMBER_ATTEST__ a')
                ->join('__MEMBER__ b')
                ->join('__ORDER__ c')
                ->field('a.id,a.member_id,a.real_name,a.rank,b.mobile,count(c.id) as total')
                ->where('c.receive_member_id = b.id and  b.id = a.member_id')->group('a.id')
                ->order('total desc')
                ->limit($page->firstRow, $page->listRows)
                ->select();

$res = M('member_attest')
                ->table('__MEMBER_ATTEST__ a')
                ->join('__MEMBER__ b ON b.id = a.member_id')
                ->join('__ORDER__ c')
                ->field('a.id,a.member_id,a.real_name,a.rank,b.mobile,count(c.id) as total')
                ->where('c.receive_member_id = b.id')->group('a.id')
                ->order('total desc')
                ->limit($page->firstRow, $page->listRows)
                ->select();

效果一样,join里面也可以不用带 on a.xx=b.xxx ,查询条件可以全部放到where里面

 

分析三

$res = M('member_attest')
                    ->table('__MEMBER_ATTEST__ a,__MEMBER__ b')
                    ->where('a.member_id = b.id')
                    ->select();

 

$res = M('member_attest a')
                ->join('__MEMBER__ b')
                ->where('a.member_id = b.id')
                ->select();
$res = M('member_attest a')
                ->join('__MEMBER__ b ON a.member_id = b.id')
                ->select();

这三个都可以正常使用

 

转载于:https://www.cnblogs.com/wesky/p/6425486.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值