Counting Instances in a table.

This is a discussion on "Counting Instances in a table." within the Databases section. This forum, and the thread "Counting Instances in a table. are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > Databases

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Mar 16th, 2006, 01:50
Reputable Member
Join Date: Sep 2005
Location: Canada, BC
Age: 24
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
Angry Counting Instances in a table.

Say I have a table something like this
Code: Select all
|----|----|
| ID |Type|
|----|----|
| 1  | a  |
|----|----|
| 2  | b  |
|----|----|
| 3  | a  |
|----|----|
| 4  | a  |
|----|----|
| 5  | b  |
|----|----|
And I want to get these results
Code: Select all
|---|---|
| a | b |
|---|---|
| 3 | 2 |
|---|---|
Is there an easy way I can do this with a single mysql query?
I would rather avoid several queries and then having to count each row with php, or doing a single query then counting the whole thing in a loop >.> I searched threw the reference manual and couldn't find anything to help me, but I'm almost dead sure their is a way to do this. Please help >.<
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Mar 16th, 2006, 02:40
Reputable Member
Join Date: Sep 2005
Location: Canada, BC
Age: 24
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Counting Instances in a table.

Found a solution that almost gives me what I want. And is close enough that I can work with it
Code: Select all
SELECT `Type` , count(*) FROM `table` GROUP BY `Type`
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Mar 18th, 2006, 03:16
Reputable Member
Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Counting Instances in a table.

Your solution is a good one, although I would count(ID) to count the number of non-NULL values in that particular column rather than counting all columns. I don't know of any way to transpose rows and columns in the results, and I do a lot of MySQL stuff!
Quote:
What makes a good signature?
That is what I would like to know.
<rant>
One that users can SEE, rather than one that gets squished by a ******ing advert from Google. Such a signature can help the reader of the thread follow up a little bit more about the questioner and person who answers him so that he can judge the quality and background of the original post and reply, and it gives at least a little something back to the person who gives his time for free to answer, in the form of a short ad.
</rant>
-- Graham
OOooo --- errrrr - looks like it might have seen my rant on this thread

Last edited by grahame; Mar 18th, 2006 at 09:12.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Mar 18th, 2006, 07:25
Reputable Member
Join Date: Sep 2005
Location: Canada, BC
Age: 24
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Counting Instances in a table.

Quote:
Originally Posted by grahame
Your solution is a good one, although I would count(ID) to count the number of non-NULL values in that particular column rather than counting all columns.
I don't understand what you mean. Just use the ID column instead of all columns? what purpose? to increase speed?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Mar 18th, 2006, 09:25
Reputable Member
Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Counting Instances in a table.

Slight increase in speed perhaps ... but the reason I do it by a particular field in other is that it gives you the flexibility to count only rows which actually have a value in the column specified. If you count a column declared as NOT NULL you get everything, of course.

Example of the extra flexibility

select agent, count(price), from hfs group by agent;

will count up the number of properties for which a price is advertised, by agent. Probably not relevant though, if you're not going to want thie extra flexibility later.

-- Graham
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Mar 18th, 2006, 17:33
Reputable Member
Join Date: Sep 2005
Location: Canada, BC
Age: 24
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Counting Instances in a table.

Not realy, but I will keep it in mind if I do need it.
Its for a game I work on, the table contains all units on a map, and I needed to know how many of each type of unit at a specific location there was.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Mar 18th, 2006, 22:16
Most Reputable Member
Join Date: Aug 2005
Location: North Wales, United Kingdom
Age: 21
Posts: 1,093
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Counting Instances in a table.

Argh online rts games
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
counting, instances, table

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Prevent multiple instances of web page duncanwill JavaScript Forum 2 May 21st, 2008 08:16
Internet Explorer not displaying some CSS in some instances! DeveloperHanson Web Page Design 2 Apr 22nd, 2008 12:19
[need help-SEO]Counting no. of backlinks RohanShenoy Search Engine Optimization (SEO) 6 Feb 25th, 2008 06:51
PHP XML Counting Elements Don Logan PHP Forum 1 Mar 18th, 2006 03:03
Counting RecordSets.... courtjester Classic ASP 5 Aug 29th, 2004 08:44


All times are GMT. The time now is 03:59.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42