问题
I want to show one custom column as alias but need to increment by using auto 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 a output with another updated packet. column with autoincrement character
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
回答1:
You can use such query to make 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:
I think your best bet is to SELECT
out the current packet table, modifying it along the way, and INSERT
it into a new table. Once you have completed this operation, you can drop 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
回答3:
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 a new row is inserted, it counts how many rows with same packetname
exist. When there is one or more, the count+9 is converted to base 36 - it's almost the same as HEX except all the way up to Z. So, if count is 1, it becomes 1+9=10=A
. The resulting value is concatenated with packetname
.
If same rows exceed 37, it will not fail, but will append 10
for 38 instead.
Keep in mind that this isn't exactly auto increment
and it could be subject to race conditions, when two users insert the same packetname
in exactly the same time, the count query can return the same value for both.
EDIT:
Note that this solution for when you'll need to insert new rows into that table afterwards and want them to have updated_packet
filled automatically. If you want to also update existing rows, one way is to create a new table with same structure, create that trigger on a new table and then do
insert into newtable(id, subid, dollar, packetname)
select id, subid, dollar, packetname from oldtable
来源:https://stackoverflow.com/questions/34329910/how-to-create-custom-column-with-auto-increment-characters