如何在mysql中生成序列号

时间:2018-03-09 14:43:49

标签: mysql sql sequence

我有以下数据:

+--------+--------+--------+--------+----------+
| IDTRX  | IDCUST | ITEM   | IDORDER| ORDERSEQ |
+--------+--------+--------+--------+----------+
|1       |  A     |  SHOES | C18001 |          |
|2       |  A     |  BAG   | C18001 |          |
|3       |  A     |  TV    | C18005 |          | 
|4       |  A     |  IPHONE| C18008 |          |
|5       |  B     |  BAG   | C18002 |          | 
|6       |  B     |  TV    | C18003 |          |
|7       |  C     |  IPHONE| C18006 |          |
+--------+--------+--------+--------+----------+

我想知道CUSTOMER项目订购的次数 如何查询以填充由IDCUST和IDORDER分组的订单序列(ORDERSEQ列)?

所以显示查询结果如下:

+--------+--------+--------+--------+----------+
| IDTRX  | IDCUST | ITEM   | IDORDER| ORDERSEQ |
+--------+--------+--------+--------+----------+
|1       |  A     |  SHOES | C18001 | ORDER-1  |
|2       |  A     |  BAG   | C18001 | ORDER-1  |
|3       |  A     |  TV    | C18005 | ORDER-2  | 
|4       |  A     |  IPHONE| C18008 | ORDER-3  |
|5       |  B     |  BAG   | C18002 | ORDER-1  | 
|6       |  B     |  TV    | C18003 | ORDER-2  |
|7       |  C     |  IPHONE| C18006 | ORDER-1  |
+--------+--------+--------+--------+----------+

2 个答案:

答案 0 :(得分:1)

一种方法使用相关子查询:

select t.*,
       (select count(distinct t2.idorder)
        from t t2
        where t2.idcust = t.idcust and t2.idtrx <= t.idtrx
       ) as orderseq
from t;

注意:这不会将值格式化为字符串。我认为整数在任何情况下都更有用(格式化为字符串很简单)。

在许多情况下,变量是一种更有效的解决方案:

select t.*,
       (@rn := if(@oc = concat_ws(':', t.idcust, t.idorder), @rn,
                  if(@oc := concat_ws(':', t.idcust, t.idorder), 1, 1)
                    )
                 )
       ) as orderseq
from (select t.*
      from t
      order by t.idcust, t.idorder, t.idtrx
     ) t cross join
     (select @oc := '', @rn := 0) params;

编辑:

您可以使用join更新表格中的列:

update t join
       (select t.*,
               (select count(distinct t2.idorder)
                from t t2
                where t2.idcust = t.idcust and t2.idtrx <= t.idtrx
               ) as new_orderseq
        from t
       ) tt
       on t.idtrx = tt.idtrx
    set orderseq = new_orderseq;  -- or whatever string formatting you want

答案 1 :(得分:0)

yesss,运作良好。

- t_orders的表结构

DROP TABLE IF EXISTS t_orders; CREATE TABLE t_orders(   IDTRX int(11)NOT NULL AUTO_INCREMENT,   IDCUST varchar(10)DEFAULT NULL,   ITEM varchar(100)DEFAULT NULL,   IDORDER varchar(10)DEFAULT NULL,   ORDERSEQ varchar(10)NOT NULL,   主要关键(IDTRX) )ENGINE = InnoDB AUTO_INCREMENT = 8 DEFAULT CHARSET = latin1;

- t_orders的记录

BEGIN; 插入t_orders VALUES(1,'A','SHOES','C18001',''); 插入t_orders VALUES(2,'A','BAG','C18001',''); 插入t_orders VALUES(3,'A','TV','C18005',''); 插入t_orders VALUES(4,'A','IPHONE','C18008',''); 插入t_orders VALUES(5,'B','BAG','C18002',''); 插入t_orders VALUES(6,'B','TV','C18003',''); 插入t_orders VALUES(7,'C','IPHONE','C18006',''); COMMIT;

此更新ORDERSEQ列的查询解决方案:

更新t_orders一个连接        (选择t。*,                (选择CONCAT('ORDER - ',count(distinct t2.IDORDER))                 来自t_orders t2                 其中t2.IDCUST = t.IDCUST和t2.IDTRX&lt; = t.IDTRX                )作为new_orderseq         来自t_orders t        )tt      在a.IDTRX = tt.IDTRX上设置a.ORDERSEQ = new_orderseq;

感谢所有

screenshoot