MySQL auto-increment based on group

后端 未结 3 848
予麋鹿
予麋鹿 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:37

    you can use a query like this:

    INSERT INTO autoinc (cid,info,customer)
    SELECT
        COALESCE(max(cid),0) +1
        , 'A Customer 1'
        , 12345
    FROM autoinc
    WHERE customer = 12345;
    

    sample

    mysql> SELECT * from autoinc;
    Empty set (0,00 sec)
    
    mysql> INSERT INTO autoinc (cid,info,customer)
        -> SELECT
        ->     COALESCE(max(cid),0) +1
        ->     , 'A Customer 1'
        ->     , 12345
        -> FROM autoinc
        -> WHERE customer = 12345;
    Query OK, 1 row affected (0,00 sec)
    Records: 1  Duplicates: 0  Warnings: 0
    
    mysql> SELECT * from autoinc;
    +----+------+--------------+----------+
    | id | cid  | info         | customer |
    +----+------+--------------+----------+
    |  1 |    1 | A Customer 1 |    12345 |
    +----+------+--------------+----------+
    1 row in set (0,00 sec)
    
    mysql> INSERT INTO autoinc (cid,info,customer)
        -> SELECT
        ->     COALESCE(max(cid),0) +1
        ->     , 'A Customer 1'
        ->     , 12345
        -> FROM autoinc
        -> WHERE customer = 12345;
    Query OK, 1 row affected (0,00 sec)
    Records: 1  Duplicates: 0  Warnings: 0
    
    mysql> SELECT * from autoinc;
    +----+------+--------------+----------+
    | id | cid  | info         | customer |
    +----+------+--------------+----------+
    |  1 |    1 | A Customer 1 |    12345 |
    |  2 |    2 | A Customer 1 |    12345 |
    +----+------+--------------+----------+
    2 rows in set (0,00 sec)
    
    mysql> INSERT INTO autoinc (cid,info,customer)
        -> SELECT
        ->     COALESCE(max(cid),0) +1
        ->     , 'B Customer 2'
        ->     , 9876
        -> FROM autoinc
        -> WHERE customer = 9876;
    Query OK, 1 row affected (0,00 sec)
    Records: 1  Duplicates: 0  Warnings: 0
    
    mysql> SELECT * from autoinc;
    +----+------+--------------+----------+
    | id | cid  | info         | customer |
    +----+------+--------------+----------+
    |  1 |    1 | A Customer 1 |    12345 |
    |  2 |    2 | A Customer 1 |    12345 |
    |  3 |    1 | B Customer 2 |     9876 |
    +----+------+--------------+----------+
    3 rows in set (0,00 sec)
    
    mysql> INSERT INTO autoinc (cid,info,customer)
        -> SELECT
        ->     COALESCE(max(cid),0) +1
        ->     , 'A Customer 1'
        ->     , 12345
        -> FROM autoinc
        -> WHERE customer = 12345;
    Query OK, 1 row affected (0,00 sec)
    Records: 1  Duplicates: 0  Warnings: 0
    
    mysql> SELECT * from autoinc;
    +----+------+--------------+----------+
    | id | cid  | info         | customer |
    +----+------+--------------+----------+
    |  1 |    1 | A Customer 1 |    12345 |
    |  2 |    2 | A Customer 1 |    12345 |
    |  3 |    1 | B Customer 2 |     9876 |
    |  4 |    3 | A Customer 1 |    12345 |
    +----+------+--------------+----------+
    4 rows in set (0,00 sec)
    
    mysql>
    

提交回复
热议问题