using COUNT for two different things in one SELECT

This is a discussion on using COUNT for two different things in one SELECT within the MySQL Database forums, part of the Database Forums category; I'd like to be able to combine these two queries into one: SELECT COUNT(id) FROM ids WHERE id = ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-30-2007
yawnmoth
 
Posts: n/a
Default using COUNT for two different things in one SELECT

I'd like to be able to combine these two queries into one:

SELECT COUNT(id) FROM ids WHERE id = 2;
SELECT COUNT(id) FROM ids WHERE id = 3;

I tried this, to no avail:

SELECT COUNT(id1.id) AS total_id1, COUNT(id2.id) AS total_id2
FROM ids AS id1, ids AS id2
WHERE id1.id = 2 AND id2.id = 3

The latter seems to produce the results of the former multiplied by
one another (eg. if the first two queries each produced 2, the latter
would produce 4).

This makes me wonder... maybe it isn't possible to combine those two
queries into one? Or am I just doing something wrong?

Reply With Quote
  #2 (permalink)  
Old 01-30-2007
Felix Geerinckx
 
Posts: n/a
Default Re: using COUNT for two different things in one SELECT

"yawnmoth" <terra1024@yahoo.com> wrote in news:1170183468.307421.214190
@s48g2000cws.googlegroups.com:

> I'd like to be able to combine these two queries into one:
>
> SELECT COUNT(id) FROM ids WHERE id = 2;
> SELECT COUNT(id) FROM ids WHERE id = 3;



SELECT
COUNT(CASE id WHEN 2 THEN 1 ELSE NULL END) AS Count_2,
COUNT(CASE id WHEN 3 THEN 1 ELSE NULL END) AS Count_3
FROM ids
WHERE id IN (2, 3);

--
felix
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 09:12 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0