我正在尝试从同一个表中选择两个数据集,而不是进行两个查询,我试图在一次调用中选择它们。
首先,我想:
SELECT COUNT(*) AS `total` FROM `Messages` WHERE `id` = '1';
,第二个是:
SELECT COUNT(*) AS `total_read` FROM `Messages` WHERE `id` = '1' AND `read` = '1';
无论如何在一个查询中执行此操作?
答案 0 :(得分:2)
SELECT
COUNT(*) total,
SUM(IF(read='1',1,0)) total_read
FROM Messages
WHERE id='1';