-->

PHP资讯

您的当前位置:首页 > 资讯 > Laravel框架

查询复杂排序

2018-09-05 18:38:07  来源:admin 点击:871

更新排名-
/*
    *  更新排名
    * @zfx
    * */
    private function updRank($testid){
        //先查出来数据
        $data = DB::select("select uid,字段A, row_number() over(order by 字段A desc) as 字段Arank,
                字段B, row_number() over(order by 字段B desc) as 字段Brank,
                字段C, row_number() over(order by 字段C desc) as 字段Crank, 
                字段D, row_number() over(order by 字段D desc) as 字段Drank from test_people where testid = ".$testid);
        //循环插入rank
        foreach ($data as $key=>$v){
            $updRank = DB::table('test_people')
                ->where('uid',$v->uid)
                ->where('testid',$testid)
                ->update([
                    '字段Arank' => $v->字段Arank,
                    '字段Brank' => $v->字段Brank,
                    '字段Crank' => $v->字段Crank,
                    '字段Drank' => $v->字段Drank
                ]);
        }
        return $updRank;
    }
    /*
    *  更新排名把最小的合并
    * @zfx
    * */
    private function updRankcheck($testid){
        //先查出来数据
        $字段Crank = DB::table('test_people')
            ->where('testid',$testid)
            ->select('字段A','字段Arank','字段B','字段Brank','字段C','字段Crank','字段D','字段Drank')
            ->get();

        foreach ($字段Crank  as $v){
            foreach ($字段Crank  as $vv){
                if($v->字段C == $vv->字段C){
                    $字段Crank_min = DB::table('test_people')->where('testid',$testid)->where('字段C',$v->字段C)->min('字段Crank');
                    $updRank = DB::table('test_people')
                        ->where('字段C',$v->字段C)
                        ->where('testid',$testid)
                        ->update([
                            '字段Crank' => $字段Crank_min
                        ]);
                }
                if($v->字段A == $vv->字段A){
                    $字段A_min = DB::table('test_people')->where('testid',$testid)->where('字段A',$v->字段A)->min('字段Arank');
                    $updRank = DB::table('test_people')
                        ->where('字段A',$v->字段A)
                        ->where('testid',$testid)
                        ->update([
                            '字段Arank' => $字段A_min,
                        ]);
                }
                if($v->字段B == $vv->字段B){
                    $字段Brank_min = DB::table('test_people')->where('testid',$testid)->where('字段B',$v->字段B)->min('字段Brank');
                    $updRank = DB::table('test_people')
                        ->where('字段B',$v->字段B)
                        ->where('testid',$testid)
                        ->update([
                            '字段Brank' => $字段Brank_min,
                        ]);
                }
                if($v->字段D == $vv->字段D){
                    $字段Drank_min = DB::table('test_people')->where('testid',$testid)->where('字段D',$v->字段D)->min('字段Drank');
                    $updRank = DB::table('test_people')
                        ->where('字段D',$v->字段D)
                        ->where('testid',$testid)
                        ->update([
                            '字段Drank' => $字段Drank_min,
                        ]);
                }
            }
        }
        return $updRank;
    }

相关文章更多 >

© 2024版权所有 水水网

电话:171780793