我有posts
表和post_tags
下面的表格是我的職位表結構示例:獨家和MySQL選擇行
post_id int(11)
post_user int(11)
post_title text
post_content longtext
,這是我post_tags結構示例:
post_id int(11)
tag_id int(11)
我需要的是從posts
表中選擇tag_id
的1和2的所有帖子,我嘗試了不同的連接,但沒有成功。 post_tags表數據的
例如:
post_id tag_id
1 1
2 1
5 2
6 1
6 2
這裏有個例子我的查詢應該返回後(從後表)衛生組織的ID是6,觀看6的例子POST_ID已經TAG_ID 1 AND TAG_ID 2 NOT ONLY他們中的一員,但在同一時間。如果你想看到從posts
的實際信息,剛剛加入該表中
select post_id
from post_tags pt
group by post_id
having sum(tag_id = 1) > 0 and
sum(tag_id = 2) > 0;
EDIT(有點的解釋):
「我試過不同的連接,但沒有成功。」......你能分享你的失敗加入! –