Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create custom column with auto-increment characters

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
like image 338
Nilesh patel Avatar asked Dec 17 '15 08:12

Nilesh patel


People also ask

How to create auto increment column in SharePoint list?

1 Create auto increment column in SharePoint list using SharePoint designer workflow. 2 Auto generate SharePoint list column using Calculated field. 3 Create auto increment column in SharePoint list using Event Receivers. 4 Create auto increment column in SharePoint list using Microsoft Flow.

How to maintain the auto increment ID with characters&integers?

I think there is no need to maintain the Auto Increment Id with Characters & integers. Simply create an Identity Column in your table which should be increment by 1. You will get the required output. Try above. Hope this will help you.

How do I add an auto increment field in MySQL?

Syntax for MySQL. The following SQL statement defines the "Personid" column to be an auto-increment primary key field in the "Persons" table: MySQL uses the AUTO_INCREMENT keyword to perform an auto-increment feature. By default, the starting value for AUTO_INCREMENT is 1, and it will increment by 1 for each new record.

What is the syntax for auto increment in SQL Server?

Syntax for SQL Server. The following SQL statement defines the "Personid" column to be an auto-increment primary key field in the "Persons" table: ); The MS SQL Server uses the IDENTITY keyword to perform an auto-increment feature. In the example above, the starting value for IDENTITY is 1, and it will increment by 1 for each new record.


2 Answers

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

like image 150
splash58 Avatar answered Oct 25 '22 22:10

splash58


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
like image 31
Tim Biegeleisen Avatar answered Oct 25 '22 22:10

Tim Biegeleisen