如何在三个不同的表上联接

时间:2019-04-02 14:55:22

标签: sql sql-server

我正在尝试用公司名称填充我的表,但是它们都来自另一个表(取决于公司类型)

例如,我当前表中有一个“ regardingobjectid”,它等于表“ opportunity”中的“ id”。在此“机会”表中没有公司名称,只有一个“ parentaccountid”,该表引用了“ account”表中的“ Id”,并且其中的名称是已知的。

因此,当前表中的AboutObjectid与另一个表中的ID匹配,我必须从另一个表中将其与ANOTHER表中的列进行匹配以获取名称。

有人知道如何做吗?我尝试了多个左联接,但我做对了。

我使用SQL Server。

这是我尝试过的一部分代码:

SELECT
e.regardingobjectid as CompanyId,
case
  when e.regardingobjectid_entitytype = 'account' then a.name
  when e.regardingobjectid_entitytype = 'lead' then l.companyname
  when e.regardingobjectid_entitytype = 'opportunity' then o.name
  when e.regardingobjectid_entitytype = 'incident' then a.name
  when e.regardingobjectid_entitytype = 'contact' then a.name
  when e.regardingobjectid_entitytype = 'salesorder' then a.name
end as Companyname
from emails e

left join opportunity o on e.regardingobjectid = o.id
left join account a on e.regardingobjectid = a.id
left join lead l on e.regardingobjectid = l.id
left join incident i on e.regardingobjectid = i.id
left join contact c on e.regardingobjectid = c.id
left join salesorder s on e.regardingobjectid = s.id

这是我的表格,用于类别客户,潜在客户和机会,但不用于类别事件,销售订单和联系人。

在表中,机会,客户和潜在客户是名称。但是,在事件表中,联系人和销售订单没有名称。来自销售订单,联系人和带有事件标签的公司的名称在帐户表中。

例如,此选择语句中的CompanyId与表事件中的“ id”相同。在此表事件中,有一个“ accountid”对应于表帐户中的“ id”,我要从中获取名称。

我设法获得了“事件”的名称,但是现在我的帐户名称不见了… 当我这样输入我的联接时:

left join crm_systemuser u on p.owneridyominame = u.fullname
left join crm_incident i on i.id = p.regardingobjectid
left join crm_account a on a.id = i.blue10_accountid
left join crm_opportunity o on o.id = p.regardingobjectid
left join crm_account s on s.id = p.regardingobjectid
left join crm_lead l on l.id = p.regardingobjectid

我的事件,线索和机会充满了名字。但是,当我切换顺序时:

  left join crm_account a on a.id = e.regardingobjectid
  left join crm_systemuser u on e.owneridyominame = u.fullname
  left join crm_incident i on i.id = e.regardingobjectid
  left join crm_account on a.id = i.blue10_accountid
  left join crm_opportunity o on o.id = e.regardingobjectid
  left join crm_lead l on l.id = e.regardingobjectid

我的事件再次是空的,但我的帐户中充满了姓名...

3 个答案:

答案 0 :(得分:0)

我相信您正在寻找一种叫做Junction Table的东西,它可以很容易地将多个表连接在一起。

Junction Table

答案 1 :(得分:0)

只需左加入链,并使用ISNULL,直到获得公司名称的值即可。

SELECT ISNULL(o.companyname, a.companyname) CompanyName
FROM myCurrentTable t
LEFT JOIN Opportunity o ON t.regardingobjectid=o.id
LEFT JOIN Account a ON o.parentaccountid=a.id

如果您知道更新示例中的值,则正确使用case语句。唯一的问题是您有一些错别字,在这种情况下使用a.name三次。试试:

SELECT
e.regardingobjectid as CompanyId,
case
  when e.regardingobjectid_entitytype = 'account' then a.name
  when e.regardingobjectid_entitytype = 'lead' then l.companyname
  when e.regardingobjectid_entitytype = 'opportunity' then o.name
  when e.regardingobjectid_entitytype = 'incident' then ai.name
  when e.regardingobjectid_entitytype = 'contact' then ac.name
  when e.regardingobjectid_entitytype = 'salesorder' then aso.name
end as Companyname
from emails e

left join opportunity o on e.regardingobjectid = o.id
left join account a on e.regardingobjectid = a.id
left join lead l on e.regardingobjectid = l.id
left join incident i on e.regardingobjectid = i.id
left join account ai on i.regardingobjectid = ai.id
left join contact c on e.regardingobjectid = c.id
left join account ac on c.regardingobjectid = ac.id
left join salesorder s on e.regardingobjectid = s.id
left join account aso on s.regardingobjectid = aso.id

CASE还有另一种格式,可能易于阅读,所以我的最终答案是:

SELECT
e.regardingobjectid as CompanyId,
case
  e.regardingobjectid_entitytype when 'account' then a.name
  when 'lead' then l.companyname
  when 'opportunity' then o.name
  when 'incident' then ai.name
  when 'contact' then ac.name
  when 'salesorder' then aso.name
end as Companyname
from emails e

left join opportunity o on e.regardingobjectid = o.id
left join account a on e.regardingobjectid = a.id
left join lead l on e.regardingobjectid = l.id
left join incident i on e.regardingobjectid = i.id
left join account ai on i.regardingobjectid = ai.id
left join contact c on e.regardingobjectid = c.id
left join account ac on c.regardingobjectid = ac.id
left join salesorder s on e.regardingobjectid = s.id
left join account aso on s.regardingobjectid = aso.id

答案 2 :(得分:0)

显示完全联接条件的简化示例

select
  e.regardingobjectid as CompanyId
  ,coalesce(o.name, a.name, l.companyname, i.name, ...) as Companyname
from emails e
left join opportunity o on o.id  = e.regardingobjectid and  e.regardingobjectid_entitytype = 'opportunity'
left join account a on a.id = e.regardingobjectid and e.regardingobjectid_entitytype = 'account'
left join lead l on l.id = e.regardingobjectid and e.regardingobjectid_entitytype = 'lead' 
left join incident i on i.id = e.regardingobjectid and e.regardingobjectid_entitytype = 'incident' 
...