1

您好我有一个包含以下信息的数据库表:

owner.primaryitowner, (bobsmith@mail.com)
owner.secondaryitowner, 
owner.primarybusinessowner, 
owner.secondarybusinessowner
users.username (email bobsmith@mail.com)
users.displayname (e.g. Bob Smith)

问题是所有者仅存储为电子邮件。我通常还有另一张桌子

inner join users on users.username = owner.primaryitowner to get users.displayname 所以数据读取正确。`

我能够做到这一点

select u.displayname
from users u
inner join owners o on
    o.primaryitowner = u.username
    or o.secondaryitowner = u.username
    or o.primarybusinessowner = u.username
    or o.secondarybusinessowner = u.username

问题是我需要有唯一的列而不是全部放在一列中。

PS 我不能更改数据库我只是一个报告作者。

非常感谢提前

4

2 回答 2

3

您需要将来自用户的每一列电子邮件加入所有者

SELECT u.displayname AS userName
   , po.displayName AS PrimaryItOwnerUsernName
   , so.displayName AS SecondaryIdOwnerUserName
FROM users AS u
INNER JOIN owners AS po on u.primaryitowner = po.username
INNER JOIN owners AS so ON u.secondaryitowner = so.username
...
WHERE u.UserName = 'Ryan J Morse'

当您多次加入所有者表(别名)时,这允许您将存储在用户中的电子邮件更改为报告所需的显示名称。

于 2012-10-25T20:22:11.753 回答
0

这对您的需求有用吗?当您从所有者表的角度来看时,将所有显示名称作为单独的列获取单个所有者记录会更容易。采用另一种方式将从同一用户的多个所有者记录中提取信息。如果这就是你想要的,那么这将行不通。

select prim_o.displayname as "Primary IT Owner",
    sec_o.displayname as "Secondary IT Owner",
    prim_bo.displayname as "Primary Business Owner",
    sec_bo.displayname as "Secondary Business Owner"
from owner o
   inner join users prim_o (o.primaryitowner = prim_o.username)
   inner join users sec_o (o.secondaryitowner = sec_o.username)
   inner join users prim_bo (o.primarybusinessowner = prim_bo.username)
   inner join users sec_bo (o.secondarybusinessowner = sec_bo.username)
于 2012-10-25T20:28:42.043 回答