我的数据库中有以下表格:
标签
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
是否可以使用 SQL 查询获取与传递的 post_id
具有相同相关标签的所有帖子?
例如:
我的帖子 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
关于mysql - 使用 JOIN 进行复杂 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36543926/