我有一个查询要查找如下所示的废弃购物车:
SELECT c.customerid,
c.custconfirstname,
c.custconemail,
o.ordstatus,
o.orddate,
GROUP_CONCAT( 'Order Id: ', orderid, ' | Product name: ', ordprodname, ' | Quantity: ', ordprodqty, '<br>' ) AS ordered_items
FROM isc_customers c
LEFT OUTER JOIN isc_orders o ON o.ordcustid = c.customerid
LEFT OUTER JOIN isc_order_products op ON op.orderorderid = o.orderid
LEFT OUTER JOIN isc_product_images pi ON pi.imageprodid = op.orderprodid
GROUP BY c.customerid
HAVING COUNT( DISTINCT o.ordcustid ) >0
AND o.ordstatus = 0
AND o.orddate < UNIX_TIMESTAMP( ) - '18000'
AND o.orddate > UNIX_TIMESTAMP( ) - '259200'
对于每个客户(唯一的 customerid),BLOB 将为ordered_items 生成类似的内容:
Order Id: 15256 | Product name: PROD A | Quantity: 1
,Order Id: 15256 | Product name: PROD B | Quantity: 1
,Order Id: 15299 | Product name: PROD A | Quantity: 1
,Order Id: 15301 | Product name: PROD A | Quantity: 1
这基本上可以解释为客户在时间范围内有 3 辆废弃的购物车。由于此查询将用于发送废弃的购物车电子邮件,因此出于各种原因,我不想发送垃圾邮件并发送包含每个废弃购物车中的每个产品的电子邮件(唯一的 orderid),包括在上面的示例中客户已经尝试过将产品 A 放入购物车 3 次超过 3 个订单,因此将在电子邮件中获得该项目 3 次。
那么如何限制查询,使其仅返回每个 customerid 1 个 orderid 的结果?