我的数据库中有以下表格:
标签
id | name
---------
1 | tag1
2 | tag2
3 | tag3
4 | tag4
map_posts_tags
post_id | tag_id
----------------
123 | 1
123 | 2
234 | 1
345 | 3
345 | 4
456 | 2
456 | 1
是否可以post_id
使用SQL查询获得与传递的标签具有相同相关标签的所有帖子?
例如:
我的ID为123的帖子,并且想要获取所有具有相同标签的相关帖子的列表(如果可能的话,从SQL列表中排除ID为123的帖子)。
SELECT * FROM tags t
INNER JOIN map_posts_tags mpt
ON t.id = mpt.tag_id
WHERE tag_id in (
select tag_id from map_posts_tags
where post_id = 123)
AND NOT post_it = 123
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句