OpensaurusRex
OpensaurusRex

Reputation: 842

How to select two different datasets with MySQL

I am trying to select two data sets from the same table, instead of doing two queries I am trying to select them both in one call.

First of all I want to:

SELECT COUNT(*) AS `total` FROM `Messages` WHERE `id` = '1';

and the second is:

SELECT COUNT(*) AS `total_read` FROM `Messages` WHERE `id` = '1' AND `read` = '1';

Is there anyway to do this in one query?

Upvotes: 0

Views: 246

Answers (1)

nosid
nosid

Reputation: 50044

SELECT
    COUNT(*) total,
    SUM(IF(read='1',1,0)) total_read
FROM Messages
WHERE id='1';

Upvotes: 2

Related Questions