Vijay
Vijay

Reputation: 5433

How to get unique values from comma separated values field?

I've a column in my table where i store the values as comma separated values,

Now i'm in need of listing all the unique values from that field/column..

How can i do this?

I'm using php..

Upvotes: 0

Views: 3237

Answers (3)

bakalek
bakalek

Reputation: 9

what, nobody storing keywords as comma separated data in the DB?

Unfortunately, the only way to quickly do it is to write a php script to explode values and than do count on distinct values in the array.

Upvotes: 0

Nickolay Olshevsky
Nickolay Olshevsky

Reputation: 14160

Storing multiple (comma-separated) values in one field is not a good database design, you should store that values in other table. In that case you'd be able to use GROUP BY SQL statement. In current situation you should select all data from table, explode() it, sort it, and drop out duplicates, which will take much more time.

Upvotes: 0

Shakti Singh
Shakti Singh

Reputation: 86336

Noway with mysql alone if you are using php then best bet is to retrieve the field and then use php to get your distinct values. explode() is a good approach, and then you could use array_unique to remove duplicate values.

https://www.php.net/manual/en/function.array-unique.php

or

You could normalize your database

Upvotes: 4

Related Questions