Discuss Help with "COUNT" :( in the MySQL Help forum on Dev Shed. Help with "COUNT" :( MySQL Help forum discussing administration, SQL syntax, and other MySQL-related topics. MySQL is an open-source relational database management system (RDBMS).
Receive the tools necessary to be the rock star of your field. Our 12-month program teaches you the evolving world of multi-channel marketing as well as the complex issues and opportunities found in the industry.
ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month! Download and Activate to enter!
Web development can be a daunting task, even for specialists. There is a lot of information to absorb and a lot of technologies to learn in order to manage a superior website. When trying to learn the ropes, developers need a reliable source to introduce new ideas that can be easily implemented. When working on large projects, even web veterans may run into a technology or an aspect of a technology that they are unfamiliar with.
Posts: 8
Time spent in forums: 3 h 9 m 8 sec
Reputation Power: 0
Help with "COUNT" :(
Alright, so I have a few fields.
type, pokemon, gender
I want to be able to retrieve the exact amount of "male" pokemon in the game, exact amount of "female" Pokemon in the game, exact amount of "Genderless" Pokemon in the game, and the exact amount of "(?) Pokemon in the game. I want to display the results in a table.
Now all these symbols are in the "gender" field. I also want to group everything by the amount of Pokemon in gaming, using ASC to display the rarest one first.
I got a bit of it to work, but I want the "type" field to be incorporated with the "Pokemon" field.
For example, if I have data like this:
Type | Pokemon | Gender
Shiny Squirtle (?)
Shiny Squirtle (?)
Squirtle M
Squirtle F
I want the query to return this:
Pokemon | M | F | G | (?) | Total ShinySquirtle 0 0 0 2 2
Squirtle 1 1 0 0 2
Would this be possible? I have tried it, but I cannot get it to work. I just need a simple query, but I also want to differentiate between different "types" of a Pokemon.
Posts: 25,046
Time spent in forums: 3 Months 2 Days 22 h 44 sec
Reputation Power: 3829
Quote:
Originally Posted by cafelatte
Be advised that, as written, this solution deviates from standard SQL:
here's a standard SQL version...
Code:
SELECT pokemon
, SUM( CASE WHEN gender = 'M'
THEN 1 ELSE 0 END ) M
, SUM( CASE WHEN gender = 'F'
THEN 1 ELSE 0 END ) F
, SUM( CASE WHEN gender = '(?)'
THEN 1 ELSE 0 END ) "(?)"
, COUNT(*) Total
FROM pokemon
GROUP
BY pokemon;
Last edited by r937 : February 3rd, 2012 at 05:04 AM.
Posts: 8
Time spent in forums: 3 h 9 m 8 sec
Reputation Power: 0
thanks for your help guys, but apparently the "type" and "Pokemon" are in 2 separate fields.
So 'ShinySquirtle' would not be in one field, it would be in 2 separate fields, as 'Shiny' is the Pokemon's 'type,' and 'Squirtle' is the Pokemon 'name.'
Any idea how I could group those together as well?
Is this what I should be doing?
Code:
$result=mysql_query("SELECT pokemon, SUM(CASE WHEN gender = 'â™' THEN 1 ELSE 0 END), SUM(CASE WHEN gender = '♀' THEN 1 ELSE 0 END), SUM(CASE WHEN gender = '(?)' THEN 1 ELSE 0 END), COUNT(*) Total FROM pokemon2 GROUP BY pokemon");
while($row = mysql_fetch_array( $result )) {
$type=$row['type'];
$pokemon=$row['pokemon'];
$genderM=$row['SUM(CASE WHEN gender = 'â™' THEN 1 ELSE 0 END)'];
$genderF=$row['SUM(CASE WHEN gender = '♀' THEN 1 ELSE 0 END)'];
$genderquestion=$row['SUM(CASE WHEN gender = '(?)' THEN 1 ELSE 0 END)'];
echo $type.$pokemon. " " .$genderM. " " .$genderF. " " .$genderquestion;
}
Posts: 25,046
Time spent in forums: 3 Months 2 Days 22 h 44 sec
Reputation Power: 3829
Quote:
Originally Posted by Shanks8124
Is this what I should be doing?
i'm pretty sure the answer is no
but then, i don't do php and this isn't the php forum
the query you should be running is this one --
Code:
SELECT pokemon
, SUM( CASE WHEN gender = 'M'
THEN 1 ELSE 0 END ) M
, SUM( CASE WHEN gender = 'F'
THEN 1 ELSE 0 END ) F
, SUM( CASE WHEN gender = '(?)'
THEN 1 ELSE 0 END ) "(?)"
, COUNT(*) Total
FROM pokemon
GROUP
BY pokemon;