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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to get most repeated values from a field

Status
Not open for further replies.

abelgv

Programmer
Mar 29, 2002
4
ES
I need to select from a Mysql database the most repeated values from a field... I mean :

---------------------
22
22
5
1
1
1
---------------------

I need to get 1,22,5 in this order. Mysql does not
allow joins.

What sql command should I use?

 
Not sure why MySQL would not allow you to join tables but if all the fields you need to query are contained in one table then maybe this example will help.

Assume you have a Database with a table called MyTable
and that it contains a field called MyValue.
Assume there are 3 records containing the value 1,
2 records containing the value 22 and 1 record containing the value 5.

The SQL could be written as:

SELECT Distinct MyValue, Count(MyValue) AS CountOfMyValue
FROM MyTable
GROUP BY MyValue
ORDER BY Count(MyValue) DESC;

This would return:

MyValue CountOfMyValue
1 3 (as in 3 instances of the value 1)
22 2 (as in 2 instances of the value 8)
5 1 (as in 1 instances of the value 5)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top