adamzwakk
adamzwakk

Reputation: 709

Display rows via comma values in MySQL

I have a comma delimited list like this saved in a mysql table:

-----------------
|id | list      |
-----------------
| 4 |12,45,65,4 |

Each number in "list" corresponds with an ID in another table.

Is there a way so I can query the other table based on theses IDs and bring up those rows associated with the numbers in "list"?

Upvotes: 0

Views: 63

Answers (1)

Emil Vikström
Emil Vikström

Reputation: 91902

Not any efficient way with your current schema. The correct and most efficient way to do it is to change the schema to hold multiple rows like this:

-----------------
|id | list      |
-----------------
| 4 |    12     |
| 4 |    45     |
| 4 |    65     |
| 4 |     4     |

Then you use JOIN operations to connect 4 to every related row in your other table.

This is called database normalization and is a very important topic in database design. Relational database systems are built to handle just this types of problems in an efficient manner.

Upvotes: 2

Related Questions