0

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.

2
  • 2 questions - 1. How many id-name pairs do you have in your actual data? 2. Can you do this replacement operation in application code instead? Commented Jun 12, 2014 at 8:31
  • @shree.pat18 Ans. 1. 100-500 data only 2. I couldnot able to do it in application. Commented Jun 12, 2014 at 8:32

2 Answers 2

3

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.

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

4 Comments

hi. I will increase the row count in table and also when I showing this table in single row it will be very difficult
@SathishBabu Absolute nonsense
@Strawberry are u suggesting me to use above concept ?
Yes. See normalization - although the id column in the example above is a little unusual. It's more common to simply store friend pairs.
0

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

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.