其中查询计数结果> 0

时间:2010-02-24 19:36:59

标签: mysql count where

我需要这样做一个查询:

SELECT wposts.ID FROM posts wposts WHERE ( SELECT COUNT(ID) FROM surveys WHERE POST_ID = wposts.ID) > 0

但需要它工作吗?

2 个答案:

答案 0 :(得分:1)

默认选择返回“set”,它与整数不兼容。 你可以这样做:

  

SELECT wposts.ID FROM posts wposts WHERE 0 NOT IN(SELECT COUNT(ID)FROM survey WHERE POST_ID = wposts.ID)

答案 1 :(得分:1)

如果外键surveys.POST_ID被编入索引,则以下SQL可以快速运行。

SELECT wposts.ID FROM posts wposts
    INNER JOIN surveys ON surveys.POST_ID = wposts.ID
GROUP BY wposts.ID