MYSQL多次连接表

发布于 2024-11-15 04:26:27 字数 497 浏览 3 评论 0原文

我有一张表,其中有几行事件,每个事件都有(在许多其他字段中)addedbyusereditedbyuserdeletedbyuserINT,并引用特定用户的users表。 我可以毫无问题地加入其中一个字段(例如 addedbyuser),我如何加入其余字段并在 php 中引用它们?

events 表:

eventid addedbyuser editedbyuser deletedbyuser
1       1           2            3

users 表:

id username
1  name1
2  name2
3  name3

所以基本上,我想显示添加、编辑和删除文章的人员的姓名,我可以在一个 SQL 查询中完成此操作吗?

I have a table, with rows of events, and each one has (amongst lots of other fields) addedbyuser, editedbyuser, deletedbyuser
There are INT, and refer back to the users table to a particular user.
I am able to join one of the fields (say addedbyuser) without any problems, how do i join the rest and reference them in php?

events table:

eventid addedbyuser editedbyuser deletedbyuser
1       1           2            3

users table:

id username
1  name1
2  name2
3  name3

So basically, I want to display the names of who added, edited and deleted the article, can I do this in one SQL query?

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

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

发布评论

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

评论(1

音栖息无 2024-11-22 04:26:27

像这样的东西:

select 
    evn.eventid, 
    us1.username as addedbyuser, 
    us2.username as editedbyuser, 
    us3.username as deletedbyuser, 
from events evn
    join users as us1 on
        evn.addedbyuser = us1.id
    join users as us2 on
        evn.editedbyuser = us2.id
    join users as us3 on
        evn.deletedbyuser = us3.id

Something like this:

select 
    evn.eventid, 
    us1.username as addedbyuser, 
    us2.username as editedbyuser, 
    us3.username as deletedbyuser, 
from events evn
    join users as us1 on
        evn.addedbyuser = us1.id
    join users as us2 on
        evn.editedbyuser = us2.id
    join users as us3 on
        evn.deletedbyuser = us3.id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文