Mysql:在一个查询中选择Count和IN子句

时间:2017-03-18 11:46:38

标签: php mysql

我有像

这样的查询
SELECT COUNT( * ) As Publishid164 FROM  `comments` WHERE  `publishid` =164

它将获得Count of One publishid = 164

但是我希望传递更多像“

”这样的笨蛋
SELECT COUNT( * ) Publishid FROM  `comments` WHERE  `publishid` IN  (164,165)

但我想要不同的发布ID

我该怎么做

提前致谢

1 个答案:

答案 0 :(得分:0)

如果您需要为每个publishid计数,则必须按

进行分组
SELECT COUNT( * )
FROM comments 
WHERE publishid IN (164,165) 
GROUP BY publishid 

如果您还需要publishid列

SELECT publishid, COUNT( * )
FROM comments 
WHERE publishid IN (164,165) 
GROUP BY publishid