Solved

Obtain Value of Max Occurrences

Posted on 2014-02-04
7
382 Views
Last Modified: 2014-02-04
Hello Experts!

Who can tell me what the most efficient way of obtaining the value of a column that occurs most often within that column?  I need something that does not require much memory or CPU.

In other words, let's say that I have a column in a table with the following values:

1
2
3
2
3
3
1
3
3
1
3
2
3

I need a mysql statement that will give me the answer of "3" for this column as the value that occurs the most times in that column.

Thank you.
0
Comment
Question by:OmniUnlimited
7 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39833059
$sql = "SELECT fname, COUNT(fname) AS num FROM my_table GROUP BY fname ORDER BY num DESC";
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39833063
The ouputs will look something like this:
USING MySQLi_Result::Fetch_Object():
stdClass Object
(
    [fname] => Ray
    [num] => 2
)

stdClass Object
(
    [fname] => Bill
    [num] => 1
)

stdClass Object
(
    [fname] => John
    [num] => 1
)
0
 
LVL 15

Expert Comment

by:Jagadishwor Dulal
ID: 39833067
SELECT colname, COUNT(*) as total
FROM tablename
GROUP BY colname;

Open in new window

0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39833087
@Jagadishwor Dulal: Have you tested that query?

@OmniUnlimited: I recommend that you test any suggestions before you accept answers!
0
 
LVL 17

Author Closing Comment

by:OmniUnlimited
ID: 39833097
Ray thank you so much!  As always, your genius just shines.

I tested the queries and only yours gave me the results I was looking for.

Have a great day!
0
 
LVL 32

Expert Comment

by:awking00
ID: 39833124
select columnname from
(select columnname, count(*) from table group by columnname order by count(*) desc)
limit 1;
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39833647
Thanks for using EE and thanks for the points and your kind words!  Best regards, ~Ray
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maybe no no httpd.conf 6 47
SELECT query on two levels (detail and summary) 13 50
How to change the link of an image using md5 in php ? 3 32
Complex MySQL Query 2 18
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question