SQL Select 关键字与用户兴趣匹配的帖子
SQL Select Posts Where Keywords Matches the Users Interests
我目前在使用我的博客网络应用程序时遇到了一些技术问题。我想制作一个发现页面,用户可以在其中根据兴趣找到新的 post。我将通过向这些 post 添加关键字来完成此操作。这样我就可以 select 用户兴趣与 post 关键字相匹配的 post。我现在才知道我误入了深水区
经过大量研究,我现在决定分享我的问题并可能获得一些指导。我有 4 tables, "users", "posts", "keywords"、"interests"。 "keywords"table包含"post_id"和"keyword_value"。 "interests"table包含"user_id"和"interest_value".
现在,我如何 select 来自 "posts" table 的 post 关键字连接到 post 匹配与当前用户相关的兴趣。
提前致谢。感谢所有回复。
数据样本:
"users"
[
"posts"
"interests"
"keywords"
如果您也提供了一些数据,那将非常有帮助。我怀疑您描述的 table 有误 - 我认为您的意思是 关键字 table 具有 post_id 和 keyword_value 和 兴趣 table 有 user_id 和 interest_value。如果是这样,那么这个查询应该有效:
SELECT *
FROM posts
WHERE post_id IN (
SELECT post_id
FROM keywords
WHERE keyword_value IN (
SELECT interest_value
FROM interests
WHERE user_id = @userId
)
)
我目前在使用我的博客网络应用程序时遇到了一些技术问题。我想制作一个发现页面,用户可以在其中根据兴趣找到新的 post。我将通过向这些 post 添加关键字来完成此操作。这样我就可以 select 用户兴趣与 post 关键字相匹配的 post。我现在才知道我误入了深水区
经过大量研究,我现在决定分享我的问题并可能获得一些指导。我有 4 tables, "users", "posts", "keywords"、"interests"。 "keywords"table包含"post_id"和"keyword_value"。 "interests"table包含"user_id"和"interest_value".
现在,我如何 select 来自 "posts" table 的 post 关键字连接到 post 匹配与当前用户相关的兴趣。
提前致谢。感谢所有回复。
数据样本:
"users"
[
"posts"
"interests"
"keywords"
如果您也提供了一些数据,那将非常有帮助。我怀疑您描述的 table 有误 - 我认为您的意思是 关键字 table 具有 post_id 和 keyword_value 和 兴趣 table 有 user_id 和 interest_value。如果是这样,那么这个查询应该有效:
SELECT *
FROM posts
WHERE post_id IN (
SELECT post_id
FROM keywords
WHERE keyword_value IN (
SELECT interest_value
FROM interests
WHERE user_id = @userId
)
)