thinkphp 3.2 多表查询 group --- 2018-11-26

分析一:

$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′),res = M('member a') ,$res = M('member as a') 意思相同

2.res = M('member')->table('__MEMBER a')  ,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();

这三个都可以正常使用

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,452评论 0 10
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 2,727评论 0 3
  • 常用语句: sql/plus sqlplus 'amdocs/Amdocs.Jx.China.110#@ysdb1...
    好好学习的蜗牛阅读 3,112评论 0 0
  • 阅读是人生最难得的一本存折,一点一滴地积累,你会发现自己是世界上最富有的人。我爱我的孩子,我一直希望能帮他建立一本...
    家有读书郎阅读 341评论 0 0
  • 大学毕业五年了,我已经在一座省城定居下来,有一份稳定的工作,在这不快不慢的节奏中过着朝九晚五的生活,除了些许时间和...
    冰城蜀黍阅读 576评论 0 4