分销中的一些数据库查询语句

查询自己所有的直接下级

select * from ims_ewei_shop_member where agentid in (select id from ims_ewei_shop_member where  openid=13)

查询自己的所有的二级下级

select * from ims_ewei_shop_member where agentid in(select id from ims_ewei_shop_member where 
agentid in (select id from ims_ewei_shop_member where  openid=13))

查询所有自己所有的三级下级

select * from ims_ewei_shop_member where agentid in(select id from ims_ewei_shop_member where 
agentid in(select id from ims_ewei_shop_member where agentid in (select id from 
ims_ewei_shop_member where  openid=13)))

ps:不知道为何一次性求出来所有的下级信息老是报错,所以用到的时候自己拼接数据

1.0求出自己所有的下级人数之和

####方法一
global $_W,$_GPC;
        $openid='wap_user_1_'; //改为$_W['openid']即可用
        $infor=pdo_get('ewei_shop_member',array('openid'=>$openid)); 
        $yiji=pdo_getall('ewei_shop_member',array('agentid'=>$infor['id']));
        $one=count($yiji);//一级人数
        $two=0;//二级人数
        $there=0;//三级人数
        foreach($yiji as $key =>$yi){
            $two1=pdo_getall('ewei_shop_member',array('agentid'=>$yi['id']));
            $two=count($two1)+$two;
            foreach($two1 as $key1 =>$tw){
                $there1=pdo_getall('ewei_shop_member',array('agentid'=>$tw['id']));
                $there=count($there1)+$there;
            }
        }
        $all=$one+$two+$there;
####方法二
global $_W,$_GPC;
        $openid='wap_user_1_18553925869';
$one=pdo_fetchall("select * from ims_ewei_shop_member where agentid in (select id from ims_ewei_shop_member where  openid='$openid')");//一级所有人数
$two=pdo_fetchall("select * from ims_ewei_shop_member where agentid in(select id from ims_ewei_shop_member where 
agentid in (select id from ims_ewei_shop_member where  openid='$openid'))");
$there=pdo_fetchall("select * from ims_ewei_shop_member where agentid in(select id from ims_ewei_shop_member where 
agentid in(select id from ims_ewei_shop_member where agentid in (select id from 
ims_ewei_shop_member where  openid='$openid')))");
$allnum=count($one)+count($two)+count($there);
$all=array_merge($one,$two,$there);
var_dump($all);
//方法二有利于求出团队所有的消费之和

团队消费总和

global $_W,$_GPC;
        $openid='wap_user_1_18553925869';
        $one=pdo_fetchall("select * from ims_ewei_shop_member where agentid in (select id from ims_ewei_shop_member where  openid='$openid')");//一级所有人数
        $two=pdo_fetchall("select * from ims_ewei_shop_member where agentid in(select id from ims_ewei_shop_member where 
        agentid in (select id from ims_ewei_shop_member where  openid='$openid'))");
        $there=pdo_fetchall("select * from ims_ewei_shop_member where agentid in(select id from ims_ewei_shop_member where 
        agentid in(select id from ims_ewei_shop_member where agentid in (select id from 
        ims_ewei_shop_member where  openid='$openid')))");
        $teamall=count($one)+count($two)+count($there);//团队总人数
        $all=array_merge($one,$two,$there);
        $teammoney=0;
        foreach($all as $key => $v){
        $order=pdo_getall('ewei_shop_order',array('status'=>3,'openid'=>$v['openid'],'uniacid'=>$_W['uniacid']));
        foreach($order as $keys =>$o){
            $teammoney=$teammoney+$o['price'];
        }
        }
$teammoney="团队消费总和";
//若是想求出其中的第一级,第二级。第三级别的消费。可将其中的$all换成one two there

一条线上的所有用户的id

    public function doMobileUup(){
            global $_W,$_GPC;
            $openid="wap_user_1_13791530157";
            $member = m("member")->getMember($openid);
            $up= $this->getSup($member['id']);
            var_dump($up);
    }
    public function getSup($id,$n = 0)
    {
        global $_W;
        global $_GPC;

        $res = pdo_get('ewei_shop_member', array('id' => $id));
        //有上级
        if ($res['agentid']!=0) {
            if($n){
                $ids .= "," . $res['agentid'];
            }else{
                $ids =$res['agentid'];
            }
            $n++;
            $ids .= $this->getSup($res['agentid'],$n);
        } 
        return $ids;
    }

你可能感兴趣的:(分销中的一些数据库查询语句)