MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMySQL Help
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.

Learn More!


Download to Enter
| Contest Rules

Tutorials | Forums

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 2nd, 2012, 01:01 PM
Shanks8124 Shanks8124 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 8 Shanks8124 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Thanks in advance.

Reply With Quote
  #2  
Old February 2nd, 2012, 01:19 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 25,046 r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 2 Days 22 h 44 sec
Reputation Power: 3829
Quote:
Originally Posted by Shanks8124
Would this be possible?
yes, but why bother?

i mean, don't you have some application language here, like php?

that's where cosmetic re-arrangement of query results should take place
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old February 2nd, 2012, 02:03 PM
Shanks8124 Shanks8124 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 8 Shanks8124 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 9 m 8 sec
Reputation Power: 0
Quote:
Originally Posted by r937
yes, but why bother?

i mean, don't you have some application language here, like php?

that's where cosmetic re-arrangement of query results should take place

Yes, I am using PHP, and I've tried echo'ing out the results, but I can't seem to get what I want.

Reply With Quote
  #4  
Old February 3rd, 2012, 04:20 AM
cafelatte cafelatte is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2008
Posts: 1,621 cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 19 h 23 m 23 sec
Reputation Power: 374
Yes, just grab the desired result set and handle the arithmetic at the application level (i.e., PHP)

If you really must do this in MySQL you can do something like the following. Be advised that, as written, this solution deviates from standard SQL:

Code:
DROP TABLE IF EXISTS pokemon;
CREATE TABLE pokemon
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,Pokemon VARCHAR(20)
,Gender VARCHAR(12)
);

INSERT INTO pokemon VALUES
(1,'Shiny Squirtle','(?)'),
(2,'Shiny Squirtle','(?)'),
(3,'Squirtle','M'),
(4,'Squirtle','F');

SELECT pokemon 
     , SUM(gender = 'M') M
     , SUM(gender = 'F') F
     , SUM(gender = '(?)') `(?)`
     , COUNT(*) Total  
  FROM pokemon 
 GROUP 
    BY pokemon;
+----------------+------+------+------+-------+
| pokemon        | M    | F    | (?)  | Total |
+----------------+------+------+------+-------+
| Shiny Squirtle |    0 |    0 |    2 |     2 |
| Squirtle       |    1 |    1 |    0 |     2 |
+----------------+------+------+------+-------+

Reply With Quote
  #5  
Old February 3rd, 2012, 04:59 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 25,046 r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level) 
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.

Reply With Quote
  #6  
Old February 3rd, 2012, 03:31 PM
Shanks8124 Shanks8124 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 8 Shanks8124 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;

}

Reply With Quote
  #7  
Old February 3rd, 2012, 05:40 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 25,046 r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level) 
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;

Reply With Quote
  #8  
Old February 4th, 2012, 03:13 AM
cafelatte cafelatte is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2008
Posts: 1,621 cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 19 h 23 m 23 sec
Reputation Power: 374
See how I provided CREATE and INSERT statements? If you think mine are wrong you're welcome to provide some of your own.

Reply With Quote
  #9  
Old February 5th, 2012, 02:26 PM
Shanks8124 Shanks8124 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 8 Shanks8124 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 9 m 8 sec
Reputation Power: 0
Quote:
Originally Posted by cafelatte
See how I provided CREATE and INSERT statements? If you think mine are wrong you're welcome to provide some of your own.


Code:
INSERT INTO pokemon2 (user_id, pokemon_id, dex, type, pokemon, gender, level, experience, item attached, roster) VALUES ('1', '', 'Shiny', 'Squirtle', 'M', '', '125', NULL, '0');


This is how it would be set up in my database.

Reply With Quote
  #10  
Old February 5th, 2012, 03:02 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 25,046 r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 2 Days 22 h 44 sec
Reputation Power: 3829
Quote:
Originally Posted by Shanks8124
This is how it would be set up in my database.
sorry, i don't buy that

that statement has at least two syntax errors

what happened when you tested the query i gave you?

Reply With Quote
  #11  
Old February 5th, 2012, 03:17 PM
Shanks8124 Shanks8124 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 8 Shanks8124 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 9 m 8 sec
Reputation Power: 0
Your Query works perfectly fine, but I also need to take the 'type' field into account. Other than that, it works brilliantly.

This would just be to show users how many Pokemon there are in game.

Reply With Quote
  #12  
Old February 5th, 2012, 03:28 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 25,046 r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 2 Days 22 h 44 sec
Reputation Power: 3829
Quote:
Originally Posted by Shanks8124
...I also need to take the 'type' field into account.
so, add it to both the SELECT clause and the GROUP BY clause

Reply With Quote
  #13  
Old February 5th, 2012, 03:33 PM
Shanks8124 Shanks8124 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 8 Shanks8124 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 9 m 8 sec
Reputation Power: 0
Quote:
Originally Posted by r937
so, add it to both the SELECT clause and the GROUP BY clause


Wow, thanks a bunch. That worked out really well.
Now I just need to find out how I can echo the results out.
Thanks again, you two.

edit: I got it to work with PHP, thanks again!

Reply With Quote
  #14  
Old February 5th, 2012, 04:47 PM
cafelatte cafelatte is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2008
Posts: 1,621 cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 19 h 23 m 23 sec
Reputation Power: 374
'thanks a bunch'

Well that's gratitude for you!!!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Help with "COUNT" :(


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.

© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap