鍍金池/ 問答/PHP  數(shù)據(jù)庫  HTML  Office/ 用最粗糙的方法,寫出了查詢簡單的商品篩選的 SQL 語句,請問我該如何簡化??

用最粗糙的方法,寫出了查詢簡單的商品篩選的 SQL 語句,請問我該如何簡化??

下面是我寫的查詢語句,可愁死我了,功能實現(xiàn)倒是實現(xiàn)了,寫的太難受了,重復好多,請問我該如何簡化呢,或者該怎么傳入變量查詢,或者有什么其他方法簡化,請舉例,或演示部分,謝謝

public function postTreat(Request $request) 
{
    $_page = $request->input("_page"); //頁碼
    $_path = $request->input("_path"); //第三級 path
    $_sortType = $request->input("_sortType"); //綜合類別

    $_sales = $request->input("_sales"); //銷售優(yōu)先
    $_priceSmall = $request->input("_priceSmall"); //最低價
    $_priceBig = $request->input("_priceBig"); //最高價

    $page=($_page-1)*4;

    // 是否有價格區(qū)間限制
    if(empty($_priceSmall)&&empty($_priceBig)){
        // 是否按銷量排序
        if(empty($_sales)){
            // 是否有綜合排序 判斷綜合類別
            if($_sortType=="composite" || $_sortType==""){ //綜合 或 沒有
                $data = DB::table('shop_goods')
                            ->where('goods_cid',$_path)
                            ->where('goods_status',1) // 0未審核 1審核通過 2審核未通過
                            ->where('goods_state',0) // 0已上架 1已下架
                            ->where('goods_recycle',0) // 0正常 1回收站
                            ->skip($page)
                            ->take(4)
                            ->get();
            }else if($_sortType=="price_up"){ //價格最低
                $data = DB::table('shop_goods')
                            ->where('goods_cid',$_path)
                            ->where('goods_status',1) // 0未審核 1審核通過 2審核未通過
                            ->where('goods_state',0) // 0已上架 1已下架
                            ->where('goods_recycle',0) // 0正常 1回收站
                            ->orderBy('goods_price','asc') // 價格最低
                            ->skip($page)
                            ->take(4)
                            ->get();
            }else if($_sortType=="price_down"){ //價格最高
                $data = DB::table('shop_goods')
                            ->where('goods_cid',$_path)
                            ->where('goods_status',1) // 0未審核 1審核通過 2審核未通過
                            ->where('goods_state',0) // 0已上架 1已下架
                            ->where('goods_recycle',0) // 0正常 1回收站
                            ->orderBy('goods_price','desc') // 價格最高 
                            ->skip($page)
                            ->take(4)
                            ->get();
            }else if($_sortType=="assess_down"){ // 評價最多
                // 只有在走這個區(qū)間的時候,才需要關(guān)聯(lián)查詢 評價的數(shù)量
                $data = DB::table('shop_goods')
                            ->leftJoin('shop_assess', 'shop_goods.goods_id', '=', 'shop_assess.assess_gcode')
                            ->selectRaw('shop_goods.*,COUNT(shop_assess.assess_id) as assess_num')
                            ->where('shop_goods.goods_cid',$_path)
                            ->where('shop_goods.goods_status',1) // 0未審核 1審核通過 2審核未通過
                            ->where('shop_goods.goods_state',0) // 0已上架 1已下架
                            ->where('shop_goods.goods_recycle',0) // 0正常 1回收站
                            ->groupBy('shop_goods.goods_id')
                            ->orderBy('assess_num','desc')
                            ->get();
            }else if($_sortType=="publish_new"){ //最新發(fā)布
                $data = DB::table('shop_goods')
                            ->where('goods_cid',$_path)
                            ->where('goods_status',1) // 0未審核 1審核通過 2審核未通過
                            ->where('goods_state',0) // 0已上架 1已下架
                            ->where('goods_recycle',0) // 0正常 1回收站
                            ->orderBy('goods_time','desc') // 最新發(fā)布
                            ->skip($page)
                            ->take(4)
                            ->get();
            }
        }else{
            $data = DB::table('shop_goods')
                        ->where('goods_cid',$_path)
                        ->where('goods_status',1) // 0未審核 1審核通過 2審核未通過
                        ->where('goods_state',0) // 0已上架 1已下架
                        ->where('goods_recycle',0) // 0正常 1回收站
                        ->orderBy('goods_num','desc') // 銷售倒序排列
                        ->skip($page)
                        ->take(4)
                        ->get();
        }
    }else{
        // 是否按銷量排序
        if(empty($_sales)){
            // 是否有綜合排序 判斷綜合類別
            if($_sortType=="composite" || $_sortType==""){
                $data = DB::table('shop_goods')
                            ->where('goods_cid',$_path)
                            ->where('goods_status',1) // 0未審核 1審核通過 2審核未通過
                            ->where('goods_state',0) // 0已上架 1已下架
                            ->where('goods_recycle',0) // 0正常 1回收站
                            ->whereBetween('goods_price',[$_priceSmall,$_priceBig]) // 價格區(qū)間
                            ->skip($page)
                            ->take(4)
                            ->get();
            }else if($_sortType=="price_up"){
                $data = DB::table('shop_goods')
                            ->where('goods_cid',$_path)
                            ->where('goods_status',1) // 0未審核 1審核通過 2審核未通過
                            ->where('goods_state',0) // 0已上架 1已下架
                            ->where('goods_recycle',0) // 0正常 1回收站
                            ->whereBetween('goods_price',[$_priceSmall,$_priceBig]) // 價格區(qū)間
                            ->orderBy('goods_price','asc') // 價格最低
                            ->skip($page)
                            ->take(4)
                            ->get();
            }else if($_sortType=="price_down"){
                $data = DB::table('shop_goods')
                            ->where('goods_cid',$_path)
                            ->where('goods_status',1) // 0未審核 1審核通過 2審核未通過
                            ->where('goods_state',0) // 0已上架 1已下架
                            ->where('goods_recycle',0) // 0正常 1回收站
                            ->whereBetween('goods_price',[$_priceSmall,$_priceBig]) // 價格區(qū)間
                            ->orderBy('goods_price','desc') // 價格最高 
                            ->skip($page)
                            ->take(4)
                            ->get();
            }else if($_sortType=="assess_down"){
                $data = DB::table('shop_goods')
                            ->leftJoin('shop_assess', 'shop_goods.goods_id', '=', 'shop_assess.assess_gcode')
                            ->selectRaw('shop_goods.*,COUNT(shop_assess.assess_id) as assess_num') //統(tǒng)計評價的數(shù)量
                            ->where('shop_goods.goods_cid',$_path)
                            ->where('shop_goods.goods_status',1) // 0未審核 1審核通過 2審核未通過
                            ->where('shop_goods.goods_state',0) // 0已上架 1已下架
                            ->where('shop_goods.goods_recycle',0) // 0正常 1回收站
                            ->whereBetween('shop_goods.goods_price',[$_priceSmall,$_priceBig]) // 價格區(qū)間
                            ->groupBy('shop_goods.goods_id')
                            ->orderBy('assess_num','desc')
                            ->get();
            }else if($_sortType=="publish_new"){
                $data = DB::table('shop_goods')
                            ->where('goods_cid',$_path)
                            ->where('goods_status',1) // 0未審核 1審核通過 2審核未通過
                            ->where('goods_state',0) // 0已上架 1已下架
                            ->where('goods_recycle',0) // 0正常 1回收站
                            ->whereBetween('goods_price',[$_priceSmall,$_priceBig]) // 價格區(qū)間
                            ->orderBy('goods_time','desc') // 最新發(fā)布
                            ->skip($page)
                            ->take(4)
                            ->get();
            }
        }else{
            $data = DB::table('shop_goods')
                        ->where('goods_cid',$_path)
                        ->where('goods_status',1) // 0未審核 1審核通過 2審核未通過
                        ->where('goods_state',0) // 0已上架 1已下架
                        ->where('goods_recycle',0) // 0正常 1回收站
                        ->whereBetween('goods_price',[$_priceSmall,$_priceBig])
                        ->orderBy('goods_num','desc')
                        ->skip($page)
                        ->take(4)
                        ->get();
        }
    }

    foreach($data as $key => $value){
        if($value->goods_num>10000){
            $value->goods_num = round(($value->goods_num)/10000,1).'萬'; //將銷量轉(zhuǎn)換
        }
    }
    return $data;
}
回答
編輯回答
萌二代

代碼重復的部分多,那么關(guān)鍵就是找出不重復的地方是什么,然后把重復的地方先弄成一塊,再按照不同的條件細分。

比如,你這上面的語句有很多按照$_sortType來判定排序的,自然你可以先用一個參數(shù)來存儲最基本的查詢語句;

$basic = DB::table('shop_goods')
            ->where('goods_cid',$_path)
            ->where('goods_status',1) // 0未審核 1審核通過 2審核未通過
            ->where('goods_state',0) // 0已上架 1已下架
            ->where('goods_recycle',0) // 0正常 1回收站

然后再根據(jù)$_sortType進行條件判斷:

if($_sortType=="composite" || $_sortType==""){ //綜合 或 沒有
    $data = $basic
                ->skip($page)
                ->take(4)
                ->get();
}else if($_sortType=="price_up"){ //價格最低
    $data = $basic
                ->orderBy('goods_price','asc') // 價格最低
                ->skip($page)
                ->take(4)
                ->get();
}else if($_sortType=="price_down"){ //價格最高
    $data = $basic
                ->orderBy('goods_price','desc') // 價格最高 
                ->skip($page)
                ->take(4)
                ->get();
}else if($_sortType=="assess_down"){ // 評價最多
    $data = DB::table('shop_goods')
                ->leftJoin('shop_assess', 'shop_goods.goods_id', '=', 'shop_assess.assess_gcode')
                ->selectRaw('shop_goods.*,COUNT(shop_assess.assess_id) as assess_num')
                ->where('shop_goods.goods_cid',$_path)
                ->where('shop_goods.goods_status',1) // 0未審核 1審核通過 2審核未通過
                ->where('shop_goods.goods_state',0) // 0已上架 1已下架
                ->where('shop_goods.goods_recycle',0) // 0正常 1回收站
                ->groupBy('shop_goods.goods_id')
                ->orderBy('assess_num','desc')
                ->get();
}else if($_sortType=="publish_new"){ //最新發(fā)布
    $data = $basic
                ->orderBy('goods_time','desc') // 最新發(fā)布
                ->skip($page)
                ->take(4)
                ->get();
}

大概的思路就是這樣。

2017年2月14日 15:35
編輯回答
做不到

我有個想法,把共用的抽離出來。我看你比較重復的東西有數(shù)據(jù)庫鏈接實例,where和order 你可以在外面做if else然后組合好where和order,除了那個要鏈接查詢的 其他的直接就可以

$db->where($where)->order($order)->skip($page)->take(4)->get();

或者你有可能是覺得代碼寫的太多的話,看起來不舒服。你可以把一些處理寫在Model層這樣的話代碼就看起來簡潔多了

2017年6月17日 17:21
編輯回答
茍活

提供一段更優(yōu)雅的代碼
寫一段大概的代碼,具體細節(jié)你需要再重寫一下

     protected $orderByMap = [
        'assess_down' => ['assess_num', 'desc'],
        'price_up' => ['goods_price', 'asc'],
        'price_down' => ['goods_price', 'desc'],
        'publish_new' => ['goods_time', 'desc'],
    ];

    /**
     * @param Request $request
     * @return mixed
     */
    public function postTreat(Request $request)
    {
        // 頁碼
        $_page = $request->input("_page");
        $page = ($_page-1)*4;

        /**
         * 構(gòu)建出一個公共的 Query
         *
         * @var $shopGoodsQuery \Illuminate\Database\Query\Builder
         */
        $shopGoodsQuery = DB::table('shop_goods')
                            ->where('goods_status', 1)// 0未審核 1審核通過 2審核未通過
                            ->where('goods_state', 0)// 0已上架 1已下架
                            ->where('goods_recycle', 0); // 0正常 1回收站;

        // 最低價
        $shopGoodsQuery->when($request->input('_priceSmall'), function (Builder $query, $priceSmall) {
            $query->where('goods_price', '>', $priceSmall);
        });

        // 最高價
        $shopGoodsQuery->when($request->input('_priceBig'), function (Builder $query, $priceBig) {
            $query->where('goods_price', '<', $priceBig);
        });

        // 是否按總和排序
        $shopGoodsQuery->when($request->input("_sales"), function (Builder $query, $sales) {
            $query->where('goods_num', 'desc');
        }, function (Builder $query, $sales) {
            // 不按總價排序的, 這個相當于 else

        });

        // 排序用 map 表映射找出字段排序
        $shopGoodsQuery->when($request->input("_sortType"), function (Builder $query, $sortType) {
            // 得到要排序的字段, 如果沒有就按默認的第一種排序
            list($field, $orderBy) = $this->orderByMap[$sortType] ?? array_first($this->orderByMap);
            $query->orderBy($field, $orderBy);
        });


        // 最后進行統(tǒng)一分頁
        $data = $shopGoodsQuery->skip($page)->take(4)->get();

        dd($data);
    }

順便發(fā)一下 Laravel 技術(shù)交流群號:584453488


補充, whenif是一樣的效果,如果沒有when就用if代替

 // 頁碼
$_page = $request->input("_page");
$page = ($_page-1)*4;

/**
 * 構(gòu)建出一個公共的 Query
 *
 * @var $shopGoodsQuery \Illuminate\Database\Query\Builder
 */
$shopGoodsQuery = DB::table('shop_goods')
                    ->where('goods_status', 1)// 0未審核 1審核通過 2審核未通過
                    ->where('goods_state', 0)// 0已上架 1已下架
                    ->where('goods_recycle', 0); // 0正常 1回收站;

// 最低價
if ($request->filled('_priceSmall')) {
    $shopGoodsQuery->where('goods_price', '>', $request->input('_priceSmall'));
}

// 最高價
if ($request->filled('_priceBig')) {
    $shopGoodsQuery->where('goods_price', '>', $request->input('_priceBig'));
}

2018年8月21日 08:50