Sathish Babu
Sathish Babu

Reputation: 285

MySQL function to replace comma separated values in column

I have one column which has comma separated values(ids). I need to change corresponding values to them. I need MySQL function for replacing.

Here my table1.

id             name
-------------------
1              Lara

2              Smith

3              Antony

4              Ken

Here my table2

id            friends
---------------------

1             2,3

2             1,2

3             1,3

4             1,4,3,2

My MySQL function (get_names) should return names with comma separated instead of name'ids when I querying my table2.

ex.

when I call like this

select get_names('1,2,3,4','Lara,Smith,Antony,Ken','3,1'); 

it should return Antony,Lara

Here

My

1st parameter is - total user ids with comma separated order

2nd parameter is - total user name with comma separated order (same order like user ids)

3rd parameter is - any no. of user ids with comma separated any order

Please help me.

Upvotes: 0

Views: 954

Answers (2)

Akhil P M
Akhil P M

Reputation: 182

comma separated values in mysql is the Violation of 1st NF

Upvotes: 0

user3722371
user3722371

Reputation: 145

I recommend changing table 2, so that you wouldn't have comma separated values:

id            friends
---------------------

1             2
1             3

2             1
2             2

3             1
3             3

4             1
4             4
4             3
4             2

In this case, you would have to change your MySQL function too, but it will be easier to retrieve values with a simple join.

Upvotes: 3

Related Questions