[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4997
  • Last Modified:

Mysql SELECT DISTINCT VALUES separated by a comma

Hi.
I have a  MySQL table, with a field named 'actors'.
In this field are found names separated by a comma.

What would be the better method to SELECT DISTINCT actors ?...
Put everything in an array and explode it then sort it? Or woud there be a simpler Query to do it?
Thank you
0
rotemweb
Asked:
rotemweb
  • 5
  • 5
1 Solution
 
Cornelia YoderArtistCommented:
SELECT DISTINCT works on the entire field, so your 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.

http://us3.php.net/manual/en/function.array-unique.php
0
 
rotemwebAuthor Commented:
So your solution would be:

- SELECT actors FROM mytable
- Then put all values in a variable (wouldn't it be too long? the table is not too big- yet)
- Explode the variable
- Use array_unique to remove duplicate

...and use that array

Is that it?  
No other solution?

Thanks
0
 
Cornelia YoderArtistCommented:
That seems like the easiest method to me.  You could normalize your database so that you have only actor name per field, and then use MySQL Select Distinct, but if you must have a variable number of comma-separated names in the table, then I don't see any better choice.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
rotemwebAuthor Commented:
Ok, thanks. I thought, maybe a special unknown Query on MySQL ;o)
But i doubted it.

Cheers :)
0
 
Cornelia YoderArtistCommented:
Thank you for the points, but I am curious (for the future) what more I could have done to earn a grade of A?
0
 
rotemwebAuthor Commented:
Well, it is a matter of how intricate is the question and the answer.
 I basically suggested the answer, and you confirmed it.
 If, thanks to your answer  I had accessed to the best solution possible, something I hadn't imagined (it happened to me here many times) I would have rated 'excellent'.
( It was very good though )

But it is not due to your knowledge (that i don't question at all);  just to the question / range of answers.  
0
 
Cornelia YoderArtistCommented:
Well, for the future, please keep in mind that sometimes the correct answer is -- there isn't a way.  

While it's true that I confirmed your thought, and added the array_unique function rather than your suggested sorting, it's not my fault that there wasn't a slick solution that you were hoping for.
0
 
rotemwebAuthor Commented:
A rating is subjective.If not, you would rate yourself.
"Flagrant injustice" doesn't apply here.
 So please let's close that topic, it is of poor interest.
Thanks.
0
 
Cornelia YoderArtistCommented:
OK, if that's the way you want to react to my suggestion for future behavior, see ya around, not.
0
 
rotemwebAuthor Commented:
Ok, you're right.
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now