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()
。是的,这更有意义,可能在这两种情况下都是这样吗?