laravel GROUP_CONCAT使用方法

DB::raw('GROUP_CONCAT(pp.sku SEPARATOR ",") sku')
<?php
      $query = ProductPool::query();
        $pageNum = !empty($parameters['per_page_num']) ? $parameters['per_page_num'] : 20;
        $start_time = $parameters['start_time'] ? $parameters['start_time'] : Date("Y-m-d", strtotime("-7 day"));
        $end_time = $parameters['end_time'] ? $parameters['end_time'] : Date("Y-m-d");
        do {
            $start_time = Date("Y-m-d", strtotime('+1 day', strtotime($start_time)));
            $time_arr[] = $start_time;
        } while ($start_time < $end_time);
        $spuArr = explode_to_arr(',', $parameters['spu']);
        $query->when($spuArr, function ($q, $spuArr) {
            return $q->whereIn('pp.spu', $spuArr);
        });
        if (!empty($parameters['done_at_start']) && !empty($parameters['done_at_end'])) {//完成时间范围
            $query->whereBetween('pp.done_at', [$parameters['done_at_start'], $parameters['done_at_end']]);
        }
        if (!empty($parameters['cost_at_start']) && !empty($parameters['cost_at_end'])) {//成本范围
            $query->whereBetween('pp.buy_price', [$parameters['cost_at_start'], $parameters['cost_at_end']]);
        }
        if (!empty($parameters['weight_at_start']) && !empty($parameters['weight_at_end'])) {//重量范围
            $query->whereBetween('ns.weight', [$parameters['weight_at_start'], $parameters['weight_at_end']]);
        }
        //1.为新品 2.不是新品
        if (!empty($parameters['is_new'])) { //是否为新品
            $times = date("Y-m-d H:i:s", strtotime("-3 month"));
            if ($parameters['is_new'] == 1) { //1 是 2 否
                $query->where('pp.done_at', '>=', $times);
            } else {
                $query->where('pp.done_at', '<', $times);
            }
        }//同时存在
        if (!empty($parameters['shop_id']) && !empty($parameters['item_id'])) {
            $productSkus = LazadaOnlineListing::from('aliexpress_online_listing AS a')
                ->leftJoin('aliexpress_online_listing_variant AS b', 'a.id', '=', 'b.listing_online_id')
                ->whereIn('a.shop_id', $parameters['shop_id'])
                ->whereIn('a.aliexpress_product_id', $parameters['item_id'])
                ->distinct()
                ->pluck('b.sku_code')
                ->toArray();
            $query->whereIn('pp.sku', $productSkus);
        } else if (!empty($parameters['item_id']) || !empty($parameters['shop_id'])) {
            $querys = LazadaOnlineListing::query();
            if (!empty($parameters['item_id'])) {
                $parameters['item_id'] = explode_to_arr(',', $parameters['item_id']);
                $querys->whereIn('a.aliexpress_product_id', $parameters['item_id']);
            }
            if (!empty($parameters['shop_id'])) {
                $querys->whereIn('a.shop_id', $parameters['shop_id'])->distinct();
            }
            $productSkuss = $querys->from('aliexpress_online_listing AS a')
                ->leftJoin('aliexpress_online_listing_variant AS b', 'a.id', '=', 'b.listing_online_id')
                ->pluck('b.sku_code')
                ->toArray();
            foreach ($productSkuss as &$parameter) {
                if (strrpos($parameter, "@#") == true) {
                    $parameter = substr($parameter, 0, strrpos($parameter, "@#"));
                }
                if (strrpos($parameter, "*") == true) {
                    $parameter = substr($parameter, 0, strrpos($parameter, "*"));
                }
                if (strrpos($parameter, "+") == true) {
                    $parameter = substr($parameter, 0, strrpos($parameter, "+"));
                }
            }
            $query->whereIn('pp.sku', $productSkuss);
        }
        $product = $query->from(DB::raw('nt_product_pool AS pp FORCE INDEX (`done_at`)'))
            ->where('pp.sale_state',1)
            ->where('pp.is_tort',0)
            ->groupBy('pp.spu')
            ->orderBy('pp.done_at', 'desc')
            ->paginate($pageNum, ['pp.spu',DB::raw('GROUP_CONCAT(pp.sku SEPARATOR ",") sku'),'pp.buy_price as price', 'pp.done_at', 'pp.pack_weight as weight']);

 

 

 以spu分组,然后把多个sku合并到一个字段里面去,用逗号分开

posted on 2021-03-25 16:12  kevin_yang123  阅读(982)  评论(0编辑  收藏  举报