|
|
 |
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
|
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 >.<
|
|
|
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
|
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`
|
|
|
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
|
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..
|
|
|
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
|
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?
|
|
|
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
|
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
|
|
|
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
|
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.
|
|
|
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
|
Re: Counting Instances in a table.
Argh online rts games 
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|