Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 两个查询优化_Mysql - Fatal编程技术网

Mysql 两个查询优化

Mysql 两个查询优化,mysql,Mysql,我有两个问题: SELECT `type`, COUNT(`id`) AS 'type' FROM `items` WHERE `post` = `id` GROUP BY `type`; SELECT `type`, COUNT(`post`!=`id`) AS 'posts' FROM `items` GROUP BY `type`; 有什么方法可以在一个查询中完成吗 像这样的怎么样 SELECT `type`, COUNT(`post`=`id`),

我有两个问题:

SELECT `type`, COUNT(`id`) AS 'type' FROM `items` WHERE `post` = `id` GROUP BY `type`;
SELECT `type`, COUNT(`post`!=`id`) AS 'posts' FROM `items` GROUP BY `type`;

有什么方法可以在一个查询中完成吗

像这样的怎么样

SELECT  `type`, 
        COUNT(`post`=`id`), 
        COUNT(`post`!=`id`) AS 'posts' 
FROM    `items` 
GROUP BY    `type`;

查询是可以的,但是这个:
COUNT(
post
=
id
),
返回的行值与
COUNT(
post
!=
id
)作为“posts”
的行值完全相同,因为
COUNT(
post
=
id
),
应该在第一行返回
5
,但是它返回
20
,与此相同:
COUNT(
post
!=
id
)作为“posts”
。您应该在那里使用
SUM()
,而不是
COUNT()
。是的,这更有意义,可能在这两种情况下都是这样吗?