如何拥有多个具有多个连接的表

发布于 2024-09-01 12:57:23 字数 1201 浏览 5 评论 0原文

我有三个表,需要将它们连接在一起并获得结果组合。我尝试过使用左/右连接,但它们没有给出所需的结果。

例如:

表 1 - STAFF

id      name
1       John
2       Fred

表 2 - STAFFMOBILERIGHTS

id      staffid     mobilerightsid      rights
--this table is empty--

表 3 - MOBILERIGHTS

id      rightname
1       Login
2       View

我需要的是这个结果...

id  name    id  staffid mobilerightsid  rights  id  rightname
1   John    null    null    null        null    1   login
1   John    null    null    null        null    2   View
2   Fred    null    null    null        null    1   login
2   Fred    null    null    null        null    2   View

我已经尝试过以下操作:

SELECT *
  FROM STAFFMOBILERIGHTS SMR
  RIGHT JOIN STAFF STA
  ON STA.STAFFID = SMR.STAFFID
  RIGHT JOIN MOBILERIGHTS MRI
  ON MRI.ID = SMR.MOBILERIGHTSID

但这仅返回两行,如下所示:

id      name    id  staffid mobilerightsid  rights  id  rightname
null    null    null    null    null        null    1   login
null    null    null    null    null        null    2   View

我想要实现的目标可以是完成了,如果是的话怎么办?

谢谢

I have three tables that I need to join together and get a combination of results. I have tried using left/right joins but they don't give the desired results.

For example:

Table 1 - STAFF

id      name
1       John
2       Fred

Table 2 - STAFFMOBILERIGHTS

id      staffid     mobilerightsid      rights
--this table is empty--

Table 3 - MOBILERIGHTS

id      rightname
1       Login
2       View

and what I need is this as the result...

id  name    id  staffid mobilerightsid  rights  id  rightname
1   John    null    null    null        null    1   login
1   John    null    null    null        null    2   View
2   Fred    null    null    null        null    1   login
2   Fred    null    null    null        null    2   View

I have tried the following :

SELECT *
  FROM STAFFMOBILERIGHTS SMR
  RIGHT JOIN STAFF STA
  ON STA.STAFFID = SMR.STAFFID
  RIGHT JOIN MOBILERIGHTS MRI
  ON MRI.ID = SMR.MOBILERIGHTSID

But this only returns two rows as follows:

id      name    id  staffid mobilerightsid  rights  id  rightname
null    null    null    null    null        null    1   login
null    null    null    null    null        null    2   View

Can what I am trying to achieve be done and if so how?

Thanks

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

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

发布评论

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

评论(2

活雷疯 2024-09-08 12:57:23

从您的评论中可以清楚地看出您想要一个交叉联接(包括所有来自工作人员和移动权利的行)。像这样的事情应该可以做到。FROM

SELECT 
*
FROM Staff, MobileRights
LEFT OUTER JOIN StaffMobileRights ON StaffMobileRights.StaffId = Staff.Id

子句指定我们将包括 Staff 表中的所有行以及 MobileRights 表中的所有行。因此,最终结果将包含 (staff * MobileRights) 行。

要从 StaffMobileRights 引入行,我们还需要连接到该表。我们使用 LEFT OUTER 连接来确保始终包含左侧(staffMobileRights 表中的行),但如果右侧(StaffMobileRights 表)不存在行,我们也不会太担心。如果连接不存在行,则返回空值。

From your comment its now clear you want a cross join (include all rows from staff and mobilerights). Something like this should do it

SELECT 
*
FROM Staff, MobileRights
LEFT OUTER JOIN StaffMobileRights ON StaffMobileRights.StaffId = Staff.Id

The FROM clause specifies that we will be including all rows from the Staff table, and all rows from the MobileRights table. The end result will therefore contain (staff * MobileRights) rows.

To bring in rows from StaffMobileRights then we need a join to that table also. We use a LEFT OUTER join to ensure that we always include the left side (rows in the staff table) but we arent too bothered if no rows exist on the right side (StaffMobileRights table). If no row exists for the join then null values are returned.

洋洋洒洒 2024-09-08 12:57:23

您可能会问的是在没有权利的地方查看 null 。在始终返回结果的矩形样式中,这是用简单联接表示它的唯一方法:

从 PaulG 的查询中,我对其进行了一些更改,以始终从 STAFF 表获取所有内容。

SELECT 
*
FROM STAFF
RIGHT OUTER JOIN StaffMobileRights ON StaffMobileRights.StaffId = Staff.Id
INNER JOIN MobileRights ON MobileRights.Id = StaffMobileRights.MobileRightsId

What you are probably asking is to see null where is no rights. In the rectangular style that results are always returned, this is the only way to represent it with a simple join:

From PaulG's query i changed it a bit to always get everything form the STAFF table.

SELECT 
*
FROM STAFF
RIGHT OUTER JOIN StaffMobileRights ON StaffMobileRights.StaffId = Staff.Id
INNER JOIN MobileRights ON MobileRights.Id = StaffMobileRights.MobileRightsId
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文