0

I have this table and I want to split it in three tables so i have names, a relation table between names and orders, and the third which contains just orders. so i think a simple way could be to add a unique number column here

+----+------+-----------------------+-------+--------------+
| id | name | address               | phone | order_number |
+----+------+-----------------------+-------+--------------+
|  1 | Joe  | Joes Address          | 1111  | 1390842      |
|  2 | Paul | Pauls Address         | 2222  | 9082309      |
|  3 | Greg | Gregs Address         | 3333  | 0928340      |
|  4 | Lucy | Lucys Address         | 4444  | 9028340      |
|  5 | Paul | Pauls Address         | 2222  | 8958399      |
|  6 | Tom  | Toms Address          | 5555  | 9084024      |
|  7 | Lucy | Lucys Another Address | 4444  | 9801983      |
|  8 | Paul | Pauls Another Address | 2222  | 0982304      |
+----+------+-----------------------+-------+--------------+

and i want to add a numeric column with a incremental number associated with the unique name value so that the expected result is

+----+------+-----------------------+-------+--------------+---+
| id | name | address               | phone | order_number |NID|
+----+------+-----------------------+-------+--------------+---|
|  1 | Joe  | Joes Address          | 1111  | 1390842      | 1 |
|  2 | Paul | Pauls Address         | 2222  | 9082309      | 2 |
|  3 | Greg | Gregs Address         | 3333  | 0928340      | 3 |
|  4 | Lucy | Lucys Address         | 4444  | 9028340      | 4 |
|  5 | Paul | Pauls Address         | 2222  | 8958399      | 2 |
|  6 | Tom  | Toms Address          | 5555  | 9084024      | 5 |
|  7 | Lucy | Lucys Another Address | 4444  | 9801983      | 4 |
|  8 | Paul | Pauls Another Address | 2222  | 0982304      | 2 |
+----+------+-----------------------+-------+--------------+---+

how can I do it??

1
  • Do you just want to display NID as query result or want to add the column in table too? Commented Oct 14, 2017 at 20:06

2 Answers 2

1

Some what similar to your desired result set by using user defined variables

select `id`, `name`, `address`, `phone`, `order_number`,
@b:= case when `name` <> @a then @b + 1 else @b end NID,
@a:= `name`
from (
    select *
    from demo b,
    (select @a:=null,@b:=1) a
    order by name 
) c

DEMO

Another simple version by assuming that id column is set to auto-increment if so then you can use correlated sub query to pick the minimum id for same name records

select a.*,
(select min(id) from demo b where a.name = b.name) nid
from demo a

Note above will not guarantee the sequence it will totally depend on id column value

DEMO

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

Comments

0

If you just want to display NIDs, then @M Khalid Junaid's answer will works fine.

But if you want to add the Column NID in the table, then following query will get the job done:

alter table t
add column nid integer;

update t
set nid = (Select 
           (case when count(name)>1 then min(id)
                 else  id
            end)
           from
           (select *from t) x
           where t.name = x.name          
           );

Note: The Nid doesn't contains incremental sequence. It is based on the id column.

Hope it helps!

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.