Question

I am working on a project where i am required to save data in the " comma separated data" form. I need to avoid data replication here. How do I do that?

Eg:

id |  name |  fav     
1  |  abc  |  11,2,3,4,5,11,2
2  |  
3  |
.
.
.

how do i display only 11,2,3,4,5 (from fav)

I used the query:


SELECT DISTINCT fav FROM user_details WHERE id = '1' ORDER BY fav ASC***

No correct solution

OTHER TIPS

You can fix this problem by storing the data in a manner appropriate for a relational database and then extracting the data in a format appropriate for presentation.

You seem to have a need for a "UserFav" table, with one row per use and one per "fav". If you had such a structure, your query would be:

select id, name, group_concat(distinct uf.favid order by uf.id) as favs
from table t join
     UserFav uf
     on t.id = uf.userid
group by t.id;

Attempting to do such string operations in the database is, shall I say, silly. Databases are designed to do some very sophisticated data manipulations. Use them. Don't try to fit a round peg into a square hole.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top