Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

comma separated value separation

Tags:

mysql

I have one table with the following kinds of records in it. I have Salesman and associated account numbers (comma separated).

+----------+----------+
| Salesman | Acct     |
+----------+----------+
| Ron      | 1,2,3,4  |
| Kin      | 6,7,8    |
| Joe      | 10,23,45 |

I am looking for output some thing like,

+----------+----------+
| Salesman | Acct     |
+----------+----------+
| Ron      | 1        |
| Ron      | 2        |
| Ron      | 3        |

Please help me write the query that can give me above result.

Thank you for your help.

like image 653
Krunal Avatar asked Feb 03 '11 19:02

Krunal


People also ask

What does enter comma separated values mean?

Comma separated values (CSV) is a term used to refer to a computer file containing tabular data that is presented in plain text. It's organised so the data can be imported into tables at a later date, e.g. in Excel. As such it's often considered a basic form of spreadsheet.


1 Answers

I assume that you need this just as a one-off operation in order to restructure your schema into something usable.

Do you have another table which lists all the possible account numbers? If you do then you can do this:

SELECT salesman, a.Acct
FROM account AS a
JOIN salesman_account AS sa
ON FIND_IN_SET(a.Acct, sa.Acct)
like image 191
Mark Byers Avatar answered Sep 22 '22 01:09

Mark Byers