MySQL auto-increment based on group

后端 未结 3 876
予麋鹿
予麋鹿 2021-01-29 05:07

The problem is related to autoincrement with mysql. What I\'m trying to achieve is to increment an ID value based on the customer number. So basically i insert data sets without

3条回答
  •  梦如初夏
    2021-01-29 05:49

    You could procedurally do this using a stored procedure, which I won't elaborate on (unless requested) as it isn't a simple query (as you're asking for).

    A hacky solution would be to bulk insert into a new joining table:

    CREATE TABLE auto_inc_customer_id (
       id INT UNSIGNED NOT NULL AUTO_INCREMENT,
       customer_id INT UNSIGNED NOT NULL, -- Could/should add a FK constraint
       PRIMARY KEY (id)
    ) ENGINE=innodb;
    
    INSERT INTO auto_inc_customer_id SELECT NULL, DISTINCT(Customer) FROM YourExistingTable;
    

    See: http://dev.mysql.com/doc/refman/5.7/en/ansi-diff-select-into-table.html

提交回复
热议问题