Skip to content

Instantly share code, notes, and snippets.

@romanlehnert
Created January 21, 2014 19:28
Show Gist options
  • Save romanlehnert/8546582 to your computer and use it in GitHub Desktop.
Save romanlehnert/8546582 to your computer and use it in GitHub Desktop.
sql query for posts with multiple comment conditions
SELECT "posts".* FROM "posts"
WHERE
"posts"."id" IN
(SELECT post_id FROM "posts"
INNER JOIN "comments" ON "comments"."post_id" = "posts"."id"
WHERE "comments"."title" = 'foo')
AND
"posts"."id" IN
(SELECT post_id FROM "posts"
INNER JOIN "comments" ON "comments"."post_id" = "posts"."id"
WHERE "comments"."title" = 'bar')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment