skip to Main Content

Is it possible to create a serial column based on another column in MySql?

Example table

reg    val    serialval
1       a      1 
1       b      2
2       z      1  
2       x      2
2       y      3 

If I insert another reg 2 the next serialval should be 4, if I insert another reg 1 the next serialval should be 3 if I insert a reg 3 the serial val must be 1 (first occurance of reg 3).

Desired output:

reg    val    serialval
1       a      1 
1       b      2
1       c      3
2       z      1  
2       x      2
2       y      3 
2       a      4
3       k      1

I’ve tried to create a storage procedure using chatGPT

DELIMITER $$

CREATE TRIGGER test_trigger
BEFORE INSERT ON my_table
FOR EACH ROW
BEGIN
    DECLARE max_serial INT;

    -- Get the maximum serial value for the current reg
    SELECT IFNULL(MAX(serialval), 0) INTO max_serial
    FROM my_table
    WHERE reg = NEW.reg;

    -- Set the serial value to max_serial + 1
    SET NEW.serialval = max_serial + 1;
END $$

DELIMITER ;

Although the above code gives me the following error:

Unknown system variable 'NEW.serialval'

2

Answers


  1. Actually, given that MySQL 8+ now supports window functions, the serialval column can possibly be generated using something like:

    SELECT reg, val, ROW_NUMBER() OVER (PARTITION BY reg ORDER BY val) serialval
    FROM my_table
    ORDER BY 1, 3;
    

    The only issue here is that, going by your sample data, the serialval should reflect the order of insertion of new data, which does not completely line up with the sort order of val. In this case, we should use this version:

    SELECT reg, val, ROW_NUMBER() OVER (PARTITION BY reg ORDER BY id) serialval
    FROM my_table
    ORDER BY 1, 3;
    

    where perhaps id is some auto incrementing sequence value.

    Login or Signup to reply.
  2. This code works fine:

    DELIMITER $$
    
    CREATE TRIGGER test_trigger
    BEFORE INSERT ON my_table
    FOR EACH ROW
    BEGIN
        -- Get the maximum serial value for the current reg
        SET @max_serial = (select IFNULL(MAX(serialval), 0) FROM my_table WHERE reg = NEW.reg);
    
        -- Set the serial value to max_serial + 1
        SET NEW.serialval = @max_serial + 1;
    END $$
    
    DELIMITER ;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search