views:

52

answers:

2

Hi all, I need to do a query like this:

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

but need it to be working ?

+1  A: 

Select returns by default a "set", which is not compatible with an integer. You can do something like:

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

Henri
+1  A: 

The following SQL works fast, if the foreign key surveys.POST_ID is indexed.

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