mysql - SQL Query to retrieve next available ID from table -
i have table contains column called ticket_id , contains values follows:
ticket_id stk0000000001 stk0000000002 stk0000000001 stk0000000003 stk0000000002 stk0000000001
the ticket_id value repeat in rows, not unique.
i using query next available id, not able working. returns stk0000000002
.
any appreciated!
sql:
select concat('stk', lpad(seq, 10, '0')) nextid (select @seq:=@seq+1 seq, num (select cast(substr(ticket_id, 4) unsigned) num sma_support_tickets union select max(cast(substr(ticket_id, 4) unsigned))+2 num sma_support_tickets order num) ids cross join (select @seq:=0) init ) pairs seq!=num limit 1
maybe i'm missing in question, seems should it:
select concat('stk', lpad(max(substring(ticket_id, 4)) + 1, 10, '0') ) sma_support_tickets;
Comments
Post a Comment