从一个表中提取数据并对照另一张表进行检查,然后显示匹配项
情况是这样的。我已经加入了网站其他部分的所有地方,一切正常,只是这个问题让我发疯。这可能很简单,我只是错过了一些东西。
这是一个用户可以登录并关注彼此博客的系统。
数据库 每个表都有不同数量的列,并且没有一个列使用相同的名称。
Table1 = userdetails(使用 ID 作为主要 ID)
Table2 = blogs(使用 BlogID 作为主要 ID) - 它具有 BlogID、WriterID blogtitle、blogtext、blogdate 等,作为行
Table3 = blogFollowers(使用 FollowBlogID 作为主要 ID),这是保存有关谁正在关注哪些博客的所有信息的 tan;e。 - 它有FollowingBlogID、TheBlogID、ImFollowing、FollowOrNot - FollowOrNot 必须为 0 或 1 - 0 等于关注 1 等于不关注)
解释 用户已登录。 $LoggedInUser = 登录时登录者的 ID。 SESSION
已登录的用户想要开始关注博客。用户单击 StartFollowing 博客链接。这会发送要插入到 Table3 中的信息。它创建一个新行(分配唯一的 id)并插入 TheBlogId(与表 1 中的 BlogID 相同)ImFollowing(插入 LoggedInUser)和 FollowOrNot 它插入 0。
示例
- User1 已登录并创建 BlogA 。
- User2 登录并在博客页面顶部看到它。
- User2 想要开始关注此博客。
- 用户 2 单击该链接。它将上面的信息插入到表 3 中。
- 然后 User2 刷新页面。现在而不是说 StartFollowing 它说停止关注。
问题:所以我无法弄清楚。
登录后,用户会在页面上看到所有博客的列表 - 博客页面(来自表 2)。每个博客标题旁边都有一个链接,显示 StartFollowing(登录用户未关注此博客)或 StopFollowing(登录用户正在关注此博客)。我怎样才能做到这一点。无论我尝试什么,我要么只显示他们已经关注的已登录用户的博客,要么只看到正在关注的博客(表3)。
Here is the situation. I have joins everywhere in other parts of the site and everything works fine, just this one hiccup that has been driving me crazy. It is probably simple and I am just missing something.
This is a system where users can log in and follow each others blogs.
The Database
Each Table has different amount of Columns and none of the Columns use the same name.
Table1 = userdetails (uses ID as Primary ID)
Table2 = blogs (uses BlogID as Primary ID)
- it has BlogID, WriterID blogtitle, blogtext, blogdate, etc. as rows
Table3 = blogFollowers (uses FollowingBlogID as primary ID) this is the tan;e that keeps all info on who is following which blogs.
- it has FollowingBlogID, TheBlogID, ImFollowing, FollowOrNot
- FollowOrNot has to be either 0 or 1 - 0 equals Following 1 equals Not Following)
EXPLANATION
User is logged in. $LoggedInUser = id of person who is logged in when they are logged in. SESSION
A logged in user want to StartFollowing a blog. The user clicks on the StartFollowing blog link. This sends info to be inserted into Table3. It creates a new row (assigns a unique id) and inserts TheBlogId (is the same as BlogID from table1) ImFollowing (inserts LoggedInUser) and FollowOrNot it inserts a 0.
EXAMPLE
- User1 is logged in and creates BlogA.
- User2 logs in and sees it at the top of the Blogs page.
- User2 wants to StartFollowing this blog.
- User2 clicks the link. It inserts the info from above into table 3.
- User2 then refreshes the page. Now instead of saying StartFollowing
it says StopFollowing.
The Problem: So what I cannot figure out.
When logged in the user sees a list of all blogs-Blog Page (From Table2) on the page. Next to each blog title there is a link that says StartFollowing (the logged in user is NOT following this blog) or StopFollowing (the logged in user IS following this blog). How can I make this happen. No matter what I try I either just show the Logged in user's blogs that they already follow OR i see only the blogs that are being followed (table3).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以这是我的看法:
首先,你说
它不应该是 table2 (粗体的)。
第二:table3 应该有一个基于
blogId
和userId
的复合主键。只有这样,用户才能关注许多博客,这才是合乎逻辑的。第三,回答你的问题:
这是一般的伪代码:
PS:如果您可以在 sql 中使用联接,您可以以更好的方式做到这一点。如果您向表结构提供代码,则可以显示查询。
So here is my take on it:
First, you said
Shouldnt it be table2 (the bold one).
Second: The table3 should have a composite primary key based on the
blogId
anduserId
. Only then will it be logical as a user can follow many blogs.Third, to answer your question:
This is the general pseudocode:
PS: You can do this in a better way if you can use joins in sql. If you provide the codes to your table structure a query can be shown.