name | id |
---|---|
Meete | 1,2 |
Reza | 2,4 |
dexer | Null |
I want to add 3,4 in id column , but number should not get repeat and keep previous values as it is
Output
name | id |
---|---|
Meete | 1,2,3,4 |
Reza | 2,3,4 |
dexer | 3,4 |
name | id |
---|---|
Meete | 1,2 |
Reza | 2,4 |
dexer | Null |
I want to add 3,4 in id column , but number should not get repeat and keep previous values as it is
Output
name | id |
---|---|
Meete | 1,2,3,4 |
Reza | 2,3,4 |
dexer | 3,4 |
This is too long for a comment.
Just don't store CSV values in a relational database! This basically defeats the purpose of a relational database. It is unsafe, unreliable, and highly inefficient.
What you ask for may look like a simple question to you, but is tedious to achieve: this typically requires splittig the string into rows, working something out, then rejoining into strings. That's a clear indication that your design is not adapted.
Each value in the CSV list should be stored on a separate row, like so:
name id
--------------
Meete 1
Meete 1
Reza 2
Reza 4
With a primary key on (name, id)
, what you ask for is as easy as:
insert into mytable (name, id)
values ('Meete', 3), ('Meete', 4), ('Reza', 3), ('Reza', 4), ('dexer', 3), ('dexer', 4)
on duplicate key update name = values(name);
And you can always generate the CVS list as needed:
select name, group_concat(id order by id) as all_ids
from mytable
group by name
Recommended reading: Is storing a delimited list in a database column really that bad?