如何从 MySQL 中的表中调用特定行? (见案例)

发布于 2024-10-05 05:22:52 字数 695 浏览 4 评论 0原文

该网站旨在让用户能够加入课程并发布对这些课程的更新,并接收其他人所做的课程更新(将课程想象为 Facebook 中的群组,但在我的网站中,用户仅通过课程发布更新

)登录网站,他应该看到他已经加入的课程中的所有更新。

我在 MySQL 中有很多表:

  • “更新”,其中具有这些属性(id,account_id,course_id,datetime,content)注意到该课程帐户 ID 是外键..
  • 具有这些属性 (id,name,..) 的“课程”
  • 具有这些属性 (id,full_name,...) 的
  • “帐户” 'accounts_courses_relationship' (account_id,course_id) 来映射用户和课程之间的关系。

我已经尝试了很多次,但我得到的唯一结果是显示所有课程的所有更新,而不排除用户不是其成员的课程的更新,如下所示:

    $sql = "SELECT DISTINCT datetime, content
    FROM updates 
    WHERE account_id != {$account_id}
    ORDER BY datetime DESC
    LIMIT 10";

那么,如何排除这些课程的更新?

注意:不要忘记用户和课程之间的所有映射都注册在上面显示的表中。

The website is intended for users to enable them joining the courses and posting their updates to these courses and also receiving the course updates made by others (imagen courses as Groups in Facebook but in my website users post updates ONLY through the courses)

When the user login to the website, he is suposed to see all the updates in the courses he already is joined in.

I have many tables in MySQL :

  • 'updates' which have these attributes (id, account_id,course_id,datetime,content) noticing that course and account ID's are foreign keys..
  • 'courses' which have these attributes (id,name,..)
  • 'accounts' which have these attributes (id,full_name,...)
  • 'accounts_courses_relationship' (account_id,course_id) , to map the relations between users and courses.

I have tried many times but the only thing I get is to show all the updates for all courses without excluding the updates from the courses that user isn't a member of, as follow:

    $sql = "SELECT DISTINCT datetime, content
    FROM updates 
    WHERE account_id != {$account_id}
    ORDER BY datetime DESC
    LIMIT 10";

So, How to exclude the updates from these courses?

Note: don't forget that all mappings between users and courses are registered in a table shown above..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

哆兒滾 2024-10-12 05:22:52

好吧,我可能没有看到你的问题,但是

SELECT ... FROM updates where account_id = {$account_id}

这不会选择当前用户的所有更新,从而选择用户感兴趣的所有更新吗?

Well i might be not seeing your problem it but how about

SELECT ... FROM updates where account_id = {$account_id}

Would't this select all updates for the current user and therefore all the updates the user is interested in?

§对你不离不弃 2024-10-12 05:22:52

像这样的东西应该有效

select * 
from updates u
join accounts_courses_relationship r on u.courseid = r.courseid
where r.account_id = {$account_id}

something like this should work

select * 
from updates u
join accounts_courses_relationship r on u.courseid = r.courseid
where r.account_id = {$account_id}
情泪▽动烟 2024-10-12 05:22:52

您必须内部联接更新和课程以及帐户和帐户_课程表。

       select acr.accountid, acr.courseid, courses.name, accounts.fullname, updates.datetime, updates.content
       from accounts_courses_relationship acr
       inner join courses on acr.courseid=courses.courseid
       inner join accounts on acr.accountid = accounts.accountid
       inner join updates on updates.accountid= accounts.accountid and updates.courseid=courses.courseid
       where accountid = {?}
       order by updates.datetime desc

You have to inner join the updates and courses and accounts and accounts_courses tables.

       select acr.accountid, acr.courseid, courses.name, accounts.fullname, updates.datetime, updates.content
       from accounts_courses_relationship acr
       inner join courses on acr.courseid=courses.courseid
       inner join accounts on acr.accountid = accounts.accountid
       inner join updates on updates.accountid= accounts.accountid and updates.courseid=courses.courseid
       where accountid = {?}
       order by updates.datetime desc
妄断弥空 2024-10-12 05:22:52

如果您有兴趣查看其他用户(不是您)的课程更新并且您是这些课程的注册用户:

SELECT DISTINCT datetime, content
FROM updates 
WHERE account_id != {$account_id}
    and course_id in (select acr.course_id from accounts_courses_relationship acr where acr.account_id = updates.account_id)
ORDER BY datetime DESC
LIMIT 10

If you're interested in seeing updates to courses from other users (not you) and you're a registered user of those courses:

SELECT DISTINCT datetime, content
FROM updates 
WHERE account_id != {$account_id}
    and course_id in (select acr.course_id from accounts_courses_relationship acr where acr.account_id = updates.account_id)
ORDER BY datetime DESC
LIMIT 10
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文