SQl GROUP_CONCAT 的使用

本文通过具体SQL语句展示了如何使用子查询来实现复杂的数据查询需求,包括根据特定条件筛选并组合数据记录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

子查询

//查询所有传入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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值