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 = ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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? |
|
|||
|
"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 |