1

我需要做这样的查询:

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

但需要它工作吗?

4

2 回答 2

1

Select 默认返回一个“集合”,它与整数不兼容。您可以执行以下操作:

选择 wposts.ID 从帖子 wposts WHERE 0 NOT IN(从调查中选择 COUNT(ID) WHERE POST_ID = wposts.ID)

于 2010-02-24T19:48:31.007 回答
1

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

SELECT wposts.ID FROM posts wposts
    INNER JOIN surveys ON surveys.POST_ID = wposts.ID
GROUP BY wposts.ID
于 2010-02-24T19:53:04.313 回答