There is the table:
id | type | serial_no |
---|---|---|
1 | apple | 1 |
2 | banana | 1 |
3 | banana | 2 |
4 | apple | 2 |
5 | water | 1 |
I want the serial_no
to be auto increment for every type
I tried
INSERT INTO MY_TBALE
(type, serial_no)
VALUES
(
apple,
(SELECT (COALESCE(MAX('type'), 0)+1 FROM MY_TABLE T1 WHERE type=apple)
)
, but it seems not working on multi-threads system.
I got some duplicate serial_no
on same type
.