Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting unique fields from MySQL 3

Status
Not open for further replies.

jimoblak

Instructor
Oct 23, 2001
3,620
US
I need to create an array ($fruits) out of the unique values found in a MySQL table.

Example:

id|value
1|apple
2|pear
3|apple
4|apple
5|orange

I am leaning toward using...
$fruits=array_unique($fruits);
sort ($fruits);

But this requires pumping a bunch of fruits into an array. I am working with 50,000+ records and this seems to be inefficient. Is there a method in MySQL (or a better one in PHP) that will trim down my list of fruits to 'apple, pear, orange'?

- - picklefish - -
 
If i'm not mistaken couldnt you just adjust your where clause in your SQL so you only get the results you want....

Something like

WHERE (((Table1.fruit)="Apple")) or (((Table1.fruit)="Orange")) ;


Would that work for you...This answer seems to simple to be right but it makes sense...
 
I've got 50,000 records with a few hundred unique items. Fruit was used as a sample to simplify the situation. It would not be practical to create SQL with hundreds of conditionals.

I hope to extract the unique 'fruits' into a PHP array so that I can then step through the mass of data and perform different actions on each group through SQL encased in a for/while/loop.

- - picklefish - -
 
You need to use the DISTINCT function in your query:
Code:
SELECT DISTINCT(myField) FROM myTable ORDER BY myField;

The result set will contain all unique entries in myField ordered alphabetically.

OK?
 
ahhh... DISTINCT - - that sounds familiar. I was too busy searching for 'unique' on mysql.com

The shameful thing is that I think I have asked the same question before. I should have remembered the answer.

Thanks

- - picklefish - -
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top