zoukankan      html  css  js  c++  java
  • thinkphp 3.2 多表查询 group

    分析一

    $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();

    这三个都可以正常使用

  • 相关阅读:
    Android学习之天气预报简单版
    Android学习之Json数据的获取与解析
    getPath()返回路径包含的“%20”(空格)的处理
    自学php 之数据库连接编程
    bnuoj 1071 拼图++
    hdu 2489 Minimal Ratio Tree
    hdu 4720 Naive and Silly Muggles
    hdu 4725 The Shortest Path in Nya Graph
    经典算法学习:排序之希尔排序(壳排序)
    经典算法学习:排序之插入排序
  • 原文地址:https://www.cnblogs.com/wesky/p/6425486.html
Copyright © 2011-2022 走看看