您好我有一个包含以下信息的数据库表:
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我无法更改数据库我只是一名报告撰稿人。
非常感谢
答案 0 :(得分: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'
当您多次加入所有者表(别名)时,您可以将存储在用户中的电子邮件更改为报告所需的显示名称。
答案 1 :(得分:0)
这是否适合您的需求?当您从所有者表的角度来看它时,更容易将所有displayname作为单个所有者记录的单独列。另一种方式是从同一用户的多个所有者记录中提取信息。如果这就是你想要的,那么这将不起作用。
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)