Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 29th, 2004, 09:52 AM
Registered User
 
Join Date: Oct 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default MySQL - Returning "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)

I'm using MySQL 4.0.20

All help appreciated

 
Old October 29th, 2004, 10:26 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Do you mean the one with the most in the table? In your example, Smith.

Yippie Ki Yea, Mr. Falcon.
 
Old October 29th, 2004, 10:39 AM
Registered User
 
Join Date: Oct 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yeah - I want one of the following

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

Cheers

 
Old November 2nd, 2004, 09:30 AM
Registered User
 
Join Date: Oct 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Anyone?

 
Old November 2nd, 2004, 10:04 AM
Registered User
 
Join Date: Oct 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

SELECT surname, count(*) cnt
FROM db
GROUP BY surname
ORDER BY cnt DESC
LIMIT 1

Did the tricks - thanks






Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting a row in MySQL database mattw1100 Beginning PHP 3 August 21st, 2008 05:28 PM
Real Reason of C# being popular than VB SoftMind BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 1 December 28th, 2006 01:48 AM
Returning the 1st row of a SELECT Sub Query rit01 SQL Server 2000 2 February 21st, 2006 12:20 PM
load form with row which is fetched from mysql ajit_kunte Beginning PHP 2 April 29th, 2005 09:07 AM
getting a specific row from a MySQL result set Tachyon Beginning PHP 3 July 12th, 2004 11:55 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.