客户订单的MYSQL顺序

时间:2017-04-22 19:01:02

标签: mysql sql

我有以下查询,我想要做的是添加一个列,以确定这对于不同的客户的订单。 entity_id对于每个订单都是唯一的,所以我想要做的就是为created_at订购的每个客户编号唯一的entity_id。

第一顺序= 1

二阶= 2

第三顺序= 3 .....

我很感激任何帮助,因为我不知道如何有效地做到这一点。

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"
    from sales_flat_order sfo, sales_flat_order_item sfoi, sales_flat_order_address sfoa        
    where       
      sfoi.order_id = sfo.entity_id AND     
      sfoa.entity_id = sfo.billing_address_id   

编辑:在以下答案之后添加数据

'2015-04-01 14:48:13','19406','201','2.0000','test@google.com','joe','vers','1'
'2015-04-01 14:48:13','19406','201','2.0000','test@google.com','joe','vers','2'
'2015-04-01 14:48:13','19406','201','2.0000','test@google.com','joe','vers','3'
'2015-04-01 14:48:13','19406','201','2.0000','test@google.com','joe','vers','4'
'2015-04-01 14:48:13','19406','201','2.0000','test@google.com','joe','vers','5'
'2015-04-01 14:48:13','19406','103','1.0000','test@google.com','joe','vers','6'
'2015-04-01 14:48:13','19406','102','1.0000','test@google.com','joe','vers','7'
'2015-04-01 14:48:13','19406','102','1.0000','test@google.com','joe','vers','8'
'2015-04-01 14:48:13','19406','102','1.0000','test@google.com','joe','vers','9'
'2015-04-01 14:48:13','19406','201','1.0000','test@google.com','joe','vers','10'
'2015-04-01 14:48:13','19406','102','1.0000','test@google.com','joe','vers','11'
'2015-04-01 14:48:13','19406','105','1.0000','test@google.com','joe','vers','12'
'2015-04-13 17:45:15','20537','105','1.0000','test@google.com','joe','vers','13'
'2015-04-13 17:45:15','20537','102','1.0000','test@google.com','joe','vers','14'
'2015-04-13 17:45:15','20537','201','1.0000','test@google.com','joe','vers','15'
'2015-04-13 17:45:15','20537','201','1.0000','test@google.com','joe','vers','16'
'2015-04-29 14:42:28','22212','102','1.0000','test@google.com','joe','vers','17'
'2015-05-11 17:11:22','23301','102','1.0000','test@google.com','joe','vers','18'

这是理想的结果

'2015-04-01 14:48:13','19406','201','2.0000','test@google.com','joe','vers','1'
'2015-04-01 14:48:13','19406','201','2.0000','test@google.com','joe','vers','1'
'2015-04-01 14:48:13','19406','201','2.0000','test@google.com','joe','vers','1'
'2015-04-01 14:48:13','19406','201','2.0000','test@google.com','joe','vers','1'
'2015-04-01 14:48:13','19406','201','2.0000','test@google.com','joe','vers','1'
'2015-04-01 14:48:13','19406','103','1.0000','test@google.com','joe','vers','1'
'2015-04-01 14:48:13','19406','102','1.0000','test@google.com','joe','vers','1'
'2015-04-01 14:48:13','19406','102','1.0000','test@google.com','joe','vers','1'
'2015-04-01 14:48:13','19406','102','1.0000','test@google.com','joe','vers','1'
'2015-04-01 14:48:13','19406','201','1.0000','test@google.com','joe','vers','1'
'2015-04-01 14:48:13','19406','102','1.0000','test@google.com','joe','vers','1'
'2015-04-01 14:48:13','19406','105','1.0000','test@google.com','joe','vers','1'
'2015-04-13 17:45:15','20537','105','1.0000','test@google.com','joe','vers','2'
'2015-04-13 17:45:15','20537','102','1.0000','test@google.com','joe','vers','2'
'2015-04-13 17:45:15','20537','201','1.0000','test@google.com','joe','vers','2'
'2015-04-13 17:45:15','20537','201','1.0000','test@google.com','joe','vers','2'
'2015-04-29 14:42:28','22212','102','1.0000','test@google.com','joe','vers','3'
'2015-05-11 17:11:22','23301','102','1.0000','test@google.com','joe','vers','4'

所以在这个例子中,所有具有entity_id 19406的行都应该具有1的seqnum,20537应该是2,依此类推。因此,如果它是相同的entity_id,它应该具有相同的seqnum。

1 个答案:

答案 0 :(得分:1)

首先。 从不FROM子句中使用逗号。 始终使用正确的JOIN语法。

二。在MySQL中执行所需操作的最简单方法是使用变量:

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;

注意:这假设customer_email定义了客户。