LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-28-2005, 04:20 PM   #1
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
quick mysql question - logic with COUNT


I have a table that contains an id and a reference to an id in another number. The id is primary.

I want to find the most frequently occuring reference id in the table and/or all references that exist more than, say, 100 times.

I've come this far:

select reference_id, COUNT(*) FROM order_products GROUP BY order_id limit 20;

but if I don't limit 20, I get lots and lots of rows. How do I only return those rows with more than, say, 100 hits? And is there a way to find the single largest?

Thanks
 
Old 09-29-2005, 11:30 AM   #2
tekkieman
Member
 
Registered: Dec 2004
Location: Northern CA
Distribution: Mepis 6.5
Posts: 123

Rep: Reputation: 15
Ok, my SQL is getting a little rusty, but in your original query, the reason you are getting so many rows is because you are basically selecting all rows from the table. You need a sub select statement to limit the return row count.

I would have to test a few queries, but you should be able to use the MAX function to return the largest count from the COUNT sub selection.

Google for SQL MAX function for some examples.

EDIT: This is generally what you're looking for...

Code:
SELECT order_id, reference_id FROM order_products
GROUP BY reference_id
HAVING COUNT(*) > 1

Last edited by tekkieman; 09-29-2005 at 12:15 PM.
 
Old 09-29-2005, 03:07 PM   #3
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Quote:
Originally posted by tekkieman
Code:
SELECT order_id, reference_id FROM order_products
GROUP BY reference_id
HAVING COUNT(*) > 1
ahh... "HAVING" - that's the operator I didn't know about.

Thanks a ton!
 
Old 09-29-2005, 04:29 PM   #4
tekkieman
Member
 
Registered: Dec 2004
Location: Northern CA
Distribution: Mepis 6.5
Posts: 123

Rep: Reputation: 15
No problem.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Quick MySQL question brandor Programming 2 03-02-2005 09:45 AM
webmin+mysql a quick question pudhiyavan Linux - Software 0 01-13-2005 08:07 PM
MySQL Select Count Question robgo777 Programming 9 01-07-2005 05:54 AM
mysql count tables in a database Longinus Programming 2 04-11-2004 01:22 AM
Should posts in general count on your post count? Joey.Dale General 16 01-27-2004 01:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration