我有两张桌子 - > USERS和POSTS-我想选择所有创建了3或4种类型的帖子的用户
这是我到目前为止所做的:
SELECT * FROM users WHERE id IN (SELECT user_id FROM posts WHERE type = "1" ) AND id IN (SELECT user_id FROM posts WHERE type = "2" ) AND id IN (SELECT user_id FROM posts WHERE type = "3" )
但我认为它不是最有效的 - 这个想法是用户从带有html复选框的表单中选择类型,然后将显示为所有类型发布帖子的帖子用户
答案 0 :(得分:0)
这应该可以解决问题:
SELECT user.*
FROM users,posts p1,posts p2,posts p3
WHERE (id=p1.user_id and p1.type ="1"
and id=p2.user_id and p2.type ="2"
and id=p3.user_id and p3.type ="3")