Click here to get back home

How to get the number of each unique items?

 HomeNewsGroups | Search

mailing.database.mysql - MySQL database issues discussed in this best MySQL group in USENET 

get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
How to get the number of each unique items? typingcat 10-28-2005
Posted by typingcat on October 28, 2005, 7:43 pm
Please log in for more thread options
Hello.
I'd like to know the SQL query for the following operation.
Table
AUTHOR BOOK
Bill A
Bill B
Charles A
Bill C
....

Output
Bill : 3 books
Charles : 1 book

I know that there is a function to get the number of matching field but
in this case I don't know what authors are in this table. Maybe I
should,
1)Get each unique authors
2)Query the number of matching items using author's name.

I don't know how to do the 1 and is there any better way? Thank you.



Posted by Aggro on October 29, 2005, 11:13 am
Please log in for more thread options
typingcat@gmail.com wrote:
show/hide quoted text

select AUTHOR,count(*) as books from yourtablename group by AUTHOR;


Posted by Aggro on October 29, 2005, 11:19 am
Please log in for more thread options
typingcat@gmail.com wrote:

show/hide quoted text

That is a very, very wrong and slow way to use database. If you need to
use loop in your program to make several queries, you are going to have
very poor performance and usually the same thing can be done with one
quick query. ( Sometimes it is faster to make multiple queries, instead
of one, but you really should know what you are doing if you do that.
And when that is the case, the database is most likely very complex and
queries need to interact with several different tables. )

I already told you how to do this with single query, but if you need to
get unique authors you could:
select AUTHOR from yourtablename group by AUTHOR;


Similar ThreadsPosted
Count Number of Items in each Category April 9, 2006, 11:47 pm
Query help: Get category and number of items in one query June 25, 2005, 1:36 am
Select last 3 items in ascending order August 2, 2006, 8:42 am
MySQL, SELECTing items with GROUP BY, HAVING COUNT(*) > 1 October 9, 2006, 10:16 pm
Help with counting total items using hierarchical data structure August 1, 2007, 1:27 am
varchar and unique December 19, 2005, 8:13 am
getting unique records November 23, 2006, 9:58 am
inserting unique value June 21, 2007, 9:20 am
Creating an unique record id ?? October 9, 2005, 1:15 pm
Returning unique records October 27, 2005, 12:35 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Driving a better car - Fuelzilla.com

Cabling site for homeowners and pros alike - Cabling-Design.com

Friends:

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap
Privacy Policy