MySQL查询 - 将BLOB的结果限制为blob(行)中的1个唯一元素

时间:2012-05-24 01:09:38

标签: php mysql

我有一个查询来查找看起来像这样的废弃购物车:

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辆废弃的推车。因为此查询将用于发送已弃用的购物车电子邮件,所以我不希望发送垃圾邮件并发送电子邮件,其中包含来自每个废弃购物车(唯一订单)的每件商品,原因有多种,包括上述示例中的客户已尝试过通过3个订单将产品A放入购物车3次,从而在电子邮件中获得3次商品。

那么如何限制查询以便它只返回每个customerid 1个orderid的结果?

1 个答案:

答案 0 :(得分:0)

在您的查询中添加DISTINCT即可。

http://www.w3schools.com/sql/sql_distinct.asp