0

我试图从同一个表中选择两个数据集,而不是做两个查询,我试图在一次调用中同时选择它们。

首先,我想:

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

第二个是:

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

无论如何可以在一个查询中执行此操作吗?

4

1 回答 1

2
SELECT
    COUNT(*) total,
    SUM(IF(read='1',1,0)) total_read
FROM Messages
WHERE id='1';
于 2012-05-24T19:09:21.240 回答