Home > Archive > MySQL Server Forum > June 2005 > Two Counts In One Query?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Two Counts In One Query?
Good Man

2005-06-17, 11:23 am

Hi there

I'm wrapping up my first *really* big, complex web application, and in
doing so, I realize that my next tast will be reading some SQL books and
learning how to improve my querying. I really didn't know that i could
work math equations, comparisons, and if clauses right in the statements
themselves.

Anyways, I digress. I'm looking for a way to query the following... I have
a database full of people, where the Membership field is either a '1' or
'0'. I'd like to get the total counts for both situations in one query.
Something that will give me:

RegisteredMembers | 332
UnregisteredMember | 543


I can obviously get this result using two queries:
+
SELECT COUNT(*) AS RegisteredMembers FROM Contacts WHERE Membership='1'
and
SELECT COUNT(*) AS UnRegisteredMembers FROM Contacts WHERE Membership='0'


but can I do this in one query?

THANKS!!
Bill Karwin

2005-06-17, 11:23 am

Good Man wrote:
> I can obviously get this result using two queries:
> +
> SELECT COUNT(*) AS RegisteredMembers FROM Contacts WHERE Membership='1'
> and
> SELECT COUNT(*) AS UnRegisteredMembers FROM Contacts WHERE Membership='0'


SELECT Membership, COUNT(*)
FROM UnRegisteredMembers
GROUP BY Membership

Regards,
Bill K.
Good Man

2005-06-17, 1:23 pm

Bill Karwin <bill@karwin.com> wrote in
news:d8utqc0bhm@enew
s3.newsguy.com:

> Good Man wrote:
>
> SELECT Membership, COUNT(*)
> FROM UnRegisteredMembers
> GROUP BY Membership


Thank you Bill.

I can't wait to sit down with a good SQL book this summer!
Sponsored Links





Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive | Programming forum archive

Copyright 2009 droptable.com