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
Actually, given that MySQL 8+ now supports window functions, the
serialval
column can possibly be generated using something like: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 ofval
. In this case, we should use this version:where perhaps
id
is some auto incrementing sequence value.This code works fine: