2017-04-22 70 views
1

我有下面的查詢,我想要做的是添加一列,將確定這是一個不同的客戶的順序。 entity_id每個訂單都是唯一的,所以我真正想要做的是爲created_at訂購的每個客戶編號唯一的entity_id。問題在於訂單中的每個商品都有一行,因此它對我當前的查詢編號錯誤。MySQL行編號沒有得到想要的結果

這是目前的結果

Order Date   ,OrderID, SKU ,Qty  ,Customer Email ,First,last ,seqnum 
'2015-04-01 14:48:13','19406','201','2.0000','[email protected]','joe','vers','1' 
'2015-04-01 14:48:13','19406','201','2.0000','[email protected]','joe','vers','2' 
'2015-04-01 14:48:13','19406','201','2.0000','[email protected]','joe','vers','3' 
'2015-04-01 14:48:13','19406','201','2.0000','[email protected]','joe','vers','4' 
'2015-04-01 14:48:13','19406','201','2.0000','[email protected]','joe','vers','5' 
'2015-04-01 14:48:13','19406','103','1.0000','[email protected]','joe','vers','6' 
'2015-04-01 14:48:13','19406','102','1.0000','[email protected]','joe','vers','7' 
'2015-04-01 14:48:13','19406','102','1.0000','[email protected]','joe','vers','8' 
'2015-04-01 14:48:13','19406','102','1.0000','[email protected]','joe','vers','9' 
'2015-04-01 14:48:13','19406','201','1.0000','[email protected]','joe','vers','10' 
'2015-04-01 14:48:13','19406','102','1.0000','[email protected]','joe','vers','11' 
'2015-04-01 14:48:13','19406','105','1.0000','[email protected]','joe','vers','12' 
'2015-04-13 17:45:15','20537','105','1.0000','[email protected]','joe','vers','13' 
'2015-04-13 17:45:15','20537','102','1.0000','[email protected]','joe','vers','14' 
'2015-04-13 17:45:15','20537','201','1.0000','[email protected]','joe','vers','15' 
'2015-04-13 17:45:15','20537','201','1.0000','[email protected]','joe','vers','16' 
'2015-04-29 14:42:28','22212','102','1.0000','[email protected]','joe','vers','17' 
'2015-05-11 17:11:22','23301','102','1.0000','[email protected]','joe','vers','18' 

這是理想的結果

Order Date   ,OrderID, SKU ,Qty  ,Customer Email ,First,last ,seqnum 
'2015-04-01 14:48:13','19406','201','2.0000','[email protected]','joe','vers','1' 
'2015-04-01 14:48:13','19406','201','2.0000','[email protected]','joe','vers','1' 
'2015-04-01 14:48:13','19406','201','2.0000','[email protected]','joe','vers','1' 
'2015-04-01 14:48:13','19406','201','2.0000','[email protected]','joe','vers','1' 
'2015-04-01 14:48:13','19406','201','2.0000','[email protected]','joe','vers','1' 
'2015-04-01 14:48:13','19406','103','1.0000','[email protected]','joe','vers','1' 
'2015-04-01 14:48:13','19406','102','1.0000','[email protected]','joe','vers','1' 
'2015-04-01 14:48:13','19406','102','1.0000','[email protected]','joe','vers','1' 
'2015-04-01 14:48:13','19406','102','1.0000','[email protected]','joe','vers','1' 
'2015-04-01 14:48:13','19406','201','1.0000','[email protected]','joe','vers','1' 
'2015-04-01 14:48:13','19406','102','1.0000','[email protected]','joe','vers','1' 
'2015-04-01 14:48:13','19406','105','1.0000','[email protected]','joe','vers','1' 
'2015-04-13 17:45:15','20537','105','1.0000','[email protected]','joe','vers','2' 
'2015-04-13 17:45:15','20537','102','1.0000','[email protected]','joe','vers','2' 
'2015-04-13 17:45:15','20537','201','1.0000','[email protected]','joe','vers','2' 
'2015-04-13 17:45:15','20537','201','1.0000','[email protected]','joe','vers','2' 
'2015-04-29 14:42:28','22212','102','1.0000','[email protected]','joe','vers','3' 
'2015-05-11 17:11:22','23301','102','1.0000','[email protected]','joe','vers','4' 

這是查詢:

select sfo.created_at AS "Order Date", sfo.entity_id AS "Order ID", 
     left(sfoi.sku,3) AS "SKU", sfoi.qty_ordered, 
     sfo.customer_email AS "Customer Email",  
     sfo.customer_firstname AS "Customer Firstname",  
     sfo.Customer_lastname AS "Customer Lastname", 
     (@rn := if(@e = sfo.customer_email, @rn + 1, 
        if(@e := sfo.customer_email, 1, 1) 
       ) 
     ) as seqnum 
from sales_flat_order sfo join 
    sales_flat_order_item sfoi 
    on sfoi.order_id = sfo.entity_id join 
    sales_flat_order_address sfoa   
    on sfoa.entity_id = sfo.billing_address_id cross join 
    (select @rn := 0, @e := '') params 
order by sfo.customer_email, created_at; 

回答

0

啊,我明白了。 訂單是基於電子郵件地址的數字。好的。

select sfo.created_at AS "Order Date", sfo.entity_id AS "Order ID", 
     left(sfoi.sku,3) AS "SKU", sfoi.qty_ordered, 
     sfo.customer_email AS "Customer Email",  
     sfo.customer_firstname AS "Customer Firstname",  
     sfo.Customer_lastname AS "Customer Lastname", 
     (@rn := if(@e = sfo.customer_email, 
        if(@o = sfo.entity_id, @rn, 
        if(@o := sfo.entity_id, @rn + 1, @rn + 1 
         ) 
        ), 
        if(@e := sfo.customer_email, 
        if(@o := sfo.entity_id, 1, 1), 
        if(@o := sfo.entity_id, 1, 1) 
        ) 
       ) 
     ) as seqnum 
from sales_flat_order sfo join 
    sales_flat_order_item sfoi 
    on sfoi.order_id = sfo.entity_id join 
    sales_flat_order_address sfoa   
    on sfoa.entity_id = sfo.billing_address_id cross join 
    (select @rn := 0, @e := '', @o := -1) params 
order by sfo.customer_email, created_at, orderid; 

這假定created_at對於每個順序是相同的。

+0

非常感謝你,如果你有時間,我會很開心,因爲我不熟悉MySql,所以我很想知道它是如何工作的。 – llerdal

+0

您需要了解如何使用變量。這是一個相當複雜的例子。 –