Submit Your Article Webforumz RegistrationAnnouncements Contact Webforumz StaffContact
Home Resources Blogs Meet the Team Contact Register
 

Go Back   WebForumz.com > Putting it Together > Databases

Reply
 
LinkBack Thread Tools
Old March 15th, 2006, 09:50 PM   #1
Reputable Member
 

Join Date: Sep 2005
Location: Canada, BC
Age: 25
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 Pheonix will become famous soon enough
Angry Counting Instances in a table.

Say I have a table something like this
Code:
|----|----|
| ID |Type|
|----|----|
| 1  | a  |
|----|----|
| 2  | b  |
|----|----|
| 3  | a  |
|----|----|
| 4  | a  |
|----|----|
| 5  | b  |
|----|----|
And I want to get these results
Code:
|---|---|
| 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 >.<
Pheonix is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old March 15th, 2006, 10:40 PM   #2
Reputable Member
 

Join Date: Sep 2005
Location: Canada, BC
Age: 25
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 Pheonix will become famous soon enough
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 `Type` , count(*) FROM `table` GROUP BY `Type`
Pheonix is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old March 17th, 2006, 11:16 PM   #3
Reputable Member
 

Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 grahame is a jewel in the rough grahame is a jewel in the rough grahame is a jewel in the rough
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; March 18th, 2006 at 05:12 AM..
grahame is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old March 18th, 2006, 03:25 AM   #4
Reputable Member
 

Join Date: Sep 2005
Location: Canada, BC
Age: 25
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 Pheonix will become famous soon enough
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?
Pheonix is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old March 18th, 2006, 05:25 AM   #5
Reputable Member
 

Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 grahame is a jewel in the rough grahame is a jewel in the rough grahame is a jewel in the rough
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
grahame is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old March 18th, 2006, 01:33 PM   #6
Reputable Member
 

Join Date: Sep 2005
Location: Canada, BC
Age: 25
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 Pheonix will become famous soon enough
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.
Pheonix is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old March 18th, 2006, 06:16 PM   #7
Most Reputable Member
 

Join Date: Aug 2005
Location: North Wales, United Kingdom
Age: 23
Posts: 1,063
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 sypher has a spectacular aura about sypher has a spectacular aura about
Re: Counting Instances in a table.

Argh online rts games
__________________
North Wales Web Design - sypher design
sypher is offline  
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

Bookmarks

Tags
counting , instances , table


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 2 May 21st, 2008 04:16 AM
Internet Explorer not displaying some CSS in some instances! DeveloperHanson HTML, XHTML and CSS 2 April 22nd, 2008 08:19 AM
[need help-SEO]Counting no. of backlinks RohanShenoy Search Engine Optimization (SEO) 6 February 25th, 2008 02:51 AM
PHP XML Counting Elements Don Logan PHP 1 March 17th, 2006 11:03 PM
Counting RecordSets.... courtjester Classic ASP 5 August 29th, 2004 04:44 AM


Search Engine Optimization by vBSEO 3.2.0 RC8