field('MIN(`id`) AS `id`, MIN(`goods_id`) AS `goods_id`, `spec_id`') ->where('type', $nType) ->where('goods_id', $goods_id) ->with([ 'Spec', 'SkuValue' => function ($query) use ($goods_id) { $query->where('goods_id', $goods_id) ->field('id,goods_id,spec_id,spec_value_id') ->with(['SpecValue']); } ])->group('spec_id')->select(); $list = collection($list)->toArray(); return $list; } /** * 批量获取SKU规格属性字符串 * @param array $sku_ids SKU ID数组 * @return array 返回 [sku_id => 'spec_name:spec_value,spec_name:spec_value'] 格式 */ public static function getSkuAttrs($sku_ids) { if (empty($sku_ids)) { return []; } // 通过shop_goods_sku表的spec_value_ids字段来查询规格信息 $list = Db::name('shop_goods_sku') ->alias('sku') ->field('sku.id, GROUP_CONCAT(sp.name,":",sv.value ORDER BY sp.id asc) as sku_attr') ->join('shop_goods_sku_spec gss', "FIND_IN_SET(gss.id, sku.spec_value_ids)", 'LEFT') ->join('shop_spec sp', 'sp.id = gss.spec_id', 'LEFT') ->join('shop_spec_value sv', 'sv.id = gss.spec_value_id', 'LEFT') ->where('sku.id', 'in', $sku_ids) ->group('sku.id') ->select(); $result = []; foreach ($list as $item) { $result[$item['id']] = $item['sku_attr'] ?: ''; } return $result; } }