SQl GROUP_CONCAT 的使用

子查询

//查询所有传入skuCode对应的SpuCode

//根据SpuCode对当前SpuCode下的所有skuCode(只取skuCode的sku_status的状态等于20的)进行分组并且拼接成字符串

 select spu_code,CONCAT('["',GROUP_CONCAT((case sku_status when 20 THEN sku_code ELSE null end
 )
Order BY id ASC SEPARATOR  '","'),'"]') from mer_goods_sku where belong_type=2 and  spu_code in(
        select spu_code from mer_goods_sku where sku_code in('10080392','1000511','1000384')) GROUP BY spu_code;

//查询所有传入skuCode对应的SpuCode

//根据SpuCode对当前SpuCode下的所有skuCode进行分组并且拼接成字符串

 select spu_code,CONCAT('["',GROUP_CONCAT( sku_code Order BY id ASC SEPARATOR  '","'),'"]') from mer_goods_sku where belong_type=2 and  spu_code in(
        select spu_code from mer_goods_sku where sku_code in('10080392','1000511','1000384')) GROUP BY spu_code;

=======================================================================>

子查询

select B.sku_code,GROUP_CONCAT(B.BigText SEPARATOR ',') from

(SELECT a.*,

CONCAT(a.is_employee,a.role) as BigText

FROM (select sku_code,id,role,case when is_employee=true then '是' else '否' end is_employee from mer_role_region_sale_visible where status =1)

AS a)

as B

group by B.sku_code;

=======================================================================> 

连表查询

select attr.sku_code as skuCode ,GROUP_CONCAT( DISTINCT attr.attribute_value_code ORDER BY attr.attribute_value_code DESC SEPARATOR ',') as attributeValueCode

FROM mer_goods_sku sku

left join mer_series_sku_attribute_value attr on sku.id = attr.sku_id

where attr.status = "1" and sku.spu_code='XG20180827000008'

GROUP BY attr.sku_code;

你可能感兴趣的:(sql优化)