How to create a custom auto-increment character column

I want to show one user column as an alias, but I need to increase it with an automatic character.

id subid dollar packetname 168 355 5813 ND-1 169 355 359 ND-1 170 356 559 ND-2 171 362 4536 ND-10 172 362 484 ND-10 134 329 4698 ND-12 135 329 435 ND-12 125 330 6293 ND-13 126 330 4293 ND-13 127 330 693 ND-13 

I need output with another column of the updated package. with auto increment symbol

 id subid dollar packetname updated packet 168 355 5813 ND-1 ND-1 169 355 359 ND-1 ND-1A 170 356 559 ND-2 ND-2 171 362 4536 ND-10 ND-10 172 362 484 ND-10 ND-10A 134 329 4698 ND-12 ND-12 135 329 435 ND-12 ND-12A 125 330 6293 ND-13 ND-13 126 330 4293 ND-13 ND-13A 127 330 693 ND-13 ND-13B 
+6
source share
3 answers

You can use this query to create an additional field

 SELECT concat(packetname, elt(if(@t=packetname, @n: =@n +1, @n:=1), '','A','B','C','D','E','F','G')) `updated packet`, id, subid, dollar, @t:=packetname packetname FROM t cross join (SELECT @n:=1, @t:="") n order by packetname 

demo on sqlfiddle

+2
source

I think that it is best to SELECT to output the current package table, modify it along the path and INSERT into a new table. After completing this operation, you can delete the original table and then rename the new one to the old one.

 INSERT INTO newpacket (id, subid, dollar, packetname, `updated packet`) SELECT p1.id, p1.subid, p1.dollar, p1.packetname, p2.`updated packet` FROM packet p1 INNER JOIN ( SELECT p.id, p.subid, CASE WHEN (SELECT p.id - MIN(t.id) FROM packet t WHERE t.subid = p.subid) > 0 THEN CONCAT(packetname, CHAR(((SELECT p.id - MIN(t.id) FROM packet t WHERE t.subid = p.subid) + 64) USING utf8)) ELSE packetname END AS `updated packet` FROM packet p ) p2 ON p1.subid = p2.subid AND p1.id = p2.id 
+2
source

You can create a trigger.

 create trigger my_trigger before insert on mytable for each row begin DECLARE samecount INT; set samecount = ( select count(*) from mytable where packetname = new.packetname ); if samecount = 0 then set new.updated_packet = new.packetname; else set new.updated_packet = concat(new.packetname,conv(samecount+9,10,36)); end if; end; 

Before inserting a new line, it counts how many lines with the same packetname exist. When there is one or more, the counter + 9 is converted to base 36 - it is almost the same as HEX, except for the entire path to Z. Thus, if the count is 1, it becomes 1+9=10=A The resulting value is combined with packetname . If the same lines exceed 37, this will not work, but will add 10 for 38 instead.

Keep in mind that this is not exactly auto increment , and this may be due to race conditions, when two users insert the same packetname exactly at the same time, the count request may return the same value for both.

EDIT: Please note that this is the solution for when you need to insert new rows into this table and want their updated_packet be populated automatically. If you want to update existing rows as well, one way is to create a new table with the same structure, create this trigger in a new table, and then do

 insert into newtable(id, subid, dollar, packetname) select id, subid, dollar, packetname from oldtable 
+1
source

All Articles