Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Returning the "most popular" row
I would like to return the most popular value of a particular field in a
mysql database:

For example:

forname, surname, county
------------------------
Alan, Smith, Wiltshire
John, Smith, Hampshire
Eric, Jones, Dorset

I would like to enter the db name (db) and fieldname (surname) and would
like the most popular surname returned (or at least a row(s) containing this
surname)

For example any of the following retirned would be superb

1. "Smith"
2. A row where the surname is "Smith" so I can extract "Smith" in PHP
3. All of the row(s) containing "Smith" so I can extract "Smith" in PHP

I'm using MySQL 4.0.20

Any comments?

Cheers



Report this thread to moderator Post Follow-up to this message
Old Post
Aaron Whiffin
11-02-04 01:56 PM


Re: Returning the "most popular" row
> I would like to enter the db name (db) and fieldname (surname) and would like the most po
pular surname returned (or at least a
> row(s) containing this surname)

If you include table name, then it could be something like:

SELECT fieldname, count(*) cnt
FROM table_name
GROUP BY fieldname
ORDER BY cnt DESC

First row of the result would be the one you look for (giving also
the count of requested value in specified table).
You can also limit the result to single row (LIMIT clause in MySQL,
TOP clause in MS SQL Server) like (I'm not sure, cause I do not
use MySQL):


SELECT fieldname, count(*) cnt
FROM db_name.table_name
GROUP BY fieldname
ORDER BY cnt DESC
LIMIT 1

Hilarion



Report this thread to moderator Post Follow-up to this message
Old Post
Hilarion
11-02-04 08:56 PM


Re: Returning the "most popular" row
One thing:
Two values may have the same top frequency, eg.:

SURNAME |  CNT
--------+------
Smith   |    7
Johnson |    7
Jones   |    5
Bond    |    1

You'll have to decide what to do in this case.

Hilarion



Report this thread to moderator Post Follow-up to this message
Old Post
Hilarion
11-02-04 08:56 PM


Re: Returning the "most popular" row

> SELECT fieldname, count(*) cnt
> FROM db_name.table_name
> GROUP BY fieldname
> ORDER BY cnt DESC
> LIMIT 1

Fantastic - perfect

Many thanks mate!



Report this thread to moderator Post Follow-up to this message
Old Post
Aaron Whiffin
11-02-04 08:56 PM


Re: Returning the "most popular" row

>
> SURNAME |  CNT
> --------+------
> Smith   |    7
> Johnson |    7
> Jones   |    5
> Bond    |    1
>
> You'll have to decide what to do in this case.

In this case it doesn't matter as it's just suggesting a category to put
something, it chooses the most popular category for each user's account.

Surnames was just an analogy

Cheers again



Report this thread to moderator Post Follow-up to this message
Old Post
Aaron Whiffin
11-02-04 08:56 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PHP SQL archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:28 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.