You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Prestashop SQL需求:查询购特定商品组合客户并展示组合信息

扩展SQL查询以显示商品属性组合

你找对方向啦!要展示客户所购指定商品的具体组合,确实需要关联ps_product_attribute_combination表。下面我给你调整现有查询,同时解释每个部分的作用:

基础版查询(直接关联组合表)

如果ps_product_attribute_combination表中已经有直接的组合名称字段(比如name),可以用这个版本:

SELECT DISTINCT 
    c.`id_customer`,
    CONCAT(c.`firstname`, ', ', c.`lastname`) AS customer,
    c.`email`,
    pac.`name` AS product_combination  -- 若实际字段不是name,替换成表中存储组合名称的字段
FROM `ps_customer` c
LEFT JOIN `ps_orders` o ON c.`id_customer` = o.`id_customer`
LEFT JOIN `ps_order_detail` od ON o.`id_order` = od.`id_order`
LEFT JOIN `ps_product_attribute_combination` pac ON od.`id_product_attribute` = pac.`id_product_attribute`
WHERE od.`product_id` = 64;

关键说明:

  • 新增LEFT JOIN关联ps_product_attribute_combination,用你提到的id_product_attribute作为关联字段,把订单明细和对应的商品组合信息绑定
  • DISTINCT会过滤掉重复的客户-组合记录,如果需要保留客户重复购买同组合的所有记录,可以去掉这个关键字

进阶版查询(拼接多属性组合)

如果商品组合是由多个属性(比如颜色、尺码)拼接而成,而ps_product_attribute_combination没有直接的组合名称,需要再关联属性表来拼接信息:

SELECT DISTINCT 
    c.`id_customer`,
    CONCAT(c.`firstname`, ', ', c.`lastname`) AS customer,
    c.`email`,
    GROUP_CONCAT(a.`name` SEPARATOR ', ') AS product_combination
FROM `ps_customer` c
LEFT JOIN `ps_orders` o ON c.`id_customer` = o.`id_customer`
LEFT JOIN `ps_order_detail` od ON o.`id_order` = od.`id_order`
LEFT JOIN `ps_product_attribute_combination` pac ON od.`id_product_attribute` = pac.`id_product_attribute`
LEFT JOIN `ps_product_attribute` pa ON pac.`id_product_attribute` = pa.`id_product_attribute`
LEFT JOIN `ps_attribute` a ON pa.`id_attribute` = a.`id_attribute`
WHERE od.`product_id` = 64
GROUP BY c.`id_customer`, customer, c.`email`, od.`id_product_attribute`;

关键说明:

  • 通过关联ps_product_attribute和ps_attribute表,获取每个组合对应的具体属性(比如“红色”“L码”)
  • GROUP_CONCAT把多个属性名称用逗号拼接成一个完整的组合描述,方便查看
  • GROUP BY确保每个客户的每个商品组合都能单独展示,不会因为多属性导致重复行

内容的提问来源于stack exchange,提问作者loid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:24:14