5

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
2
  • 2
    Hmm, suppose 37th column will be ND-13Z, what would you expect 38th to be? Commented Dec 17, 2015 at 8:49
  • @Timekiller no I am not expect anything on 38th. My max requirement is A to G only. Commented Dec 17, 2015 at 9:01

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

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

4 Comments

Is it safe to operate on id/subid though? What if id values are sparse in real data? What if there are same packetname for different subid?
I'm assuming that id - subid combinations are unique. I think we need to make some assumptions to even guess a solution here. By the way, @Timekiller is an appropriate name. I have already spent one hour on this problem.
Well what error do you get? SQLFiddle has been down for a while now.
I was missing a condition in the ON clause. Could you please try one more time?
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.