SQL Server - 跨多个表

时间:2016-10-31 21:43:35

标签: sql-server tsql

以下是相关表格信息:

tgenie

Guid | genietypeguid | ...

和:

tgenieType

guid | genietype | ...

我做了这个查询以选择所有可能的条目:

select 
    tsearch.description,
    tcompany.CompanyName,
    tsearch.CompanyGUID,
    tgenie.GenieNotes,
    tGenieType.GenieType
from 
    tcompany, tsearch, tGenieType
left outer join 
    tGenie on tgenie.GenieTypeGUID = tGenieType.GUID

结果我得到了这个输出:

description | CompanyName | CompanyGUID | GenieNotes | GenieType

VP Buiness AP Dev | Test |  5920B842-8216-45AA-A2AF-EA69740959FC |  Test2 | Add'l Candidate Info
VP Buiness AP Dev | Test |  5920B842-8216-45AA-A2AF-EA69740959FC |  Test3 | Add'l Candidate Info
VP Buiness AP Dev | Test |  5920B842-8216-45AA-A2AF-EA69740959FC |  Test5 | Add'l Candidate Info

。 。

然而,它并没有像预期的那样起作用。

GenieType设置为"添加' l候选信息"对于所有条目。我已经运行了很长时间的查询,但对于所有行似乎都是一样的...这似乎不对。

任何团体都知道为什么我的加入不起作用?

提前致谢!

更新1

我最终得到了这个查询

花了9分钟,有1478028行...

select tsearch.Description,

tcompany.CompanyName,
tcompany.GUID as CompanyGuid,
tcompanylocation.LocationName,
tsearchtype.SearchType,
tsearchresult.searchresult,
tpeople.GUID as PersonPlacedGuid,
tpeople.LastName As PersonPlacedLName,
tpeople.Firstname As PersonPlacedFName,
tpeople.address1,
 tpeople.address2,
 tpeople.city,
 tpeople.zipcode,
 tpeople.city2,
 tpeople.zipcode2,
 tpeople.emailaddress,
 tpeople.website,
 tpeople.homephone,
 tpeople.OtherPhoneDescription1,
 tpeople.otherphone1,
 tpeople.otherphonedescription3,
 tpeople.otherphone3,
 tpeople.resumefile,
 tpeople.resumeocr,
 tpeople.timeentered,
 tPeopleEducation.education,
tPeopleEducation.GradYear,
tsearch.SearchNotes,
( select tpeople.Firstname  from tpeople where tpeople.guid=tSearch.RepresentativeGUID) as repfirstname, 
( select tpeople.Lastname from tpeople where tpeople.guid=tSearch.RepresentativeGUID) as replastname,
tsearch.RepresentativeGUID as RepGuid,
tposition.Position as backgroundposition,
tdepartment.Department as backgrounddepartment,
( select tpeople.Lastname from tpeople where tpeople.guid=tSearch.ReferredByGUID) as referredbylastname,
( select tpeople.FirstName from tpeople where tpeople.guid=tSearch.ReferredByGUID) as referredbylastname,
tsearch.ReferredByGUID as PersonwhorefferedGuid,
( select tcompany.CompanyName from tCompany where tCompany.guid=tSearch.PlacedFromGUID) as placedfrom,
tinstantstatustype.InstantStatus,
tWorkbench.WorkbenchName,
( select tpeople.Lastname from tpeople where tpeople.guid=tInstantStatus.PeopleGUID) as Candlastname,
( select tpeople.FirstName from tpeople where tpeople.guid=tInstantStatus.PeopleGUID) as candFirstname,
tInstantStatus.ForClientNotes,
tinstantstatus.InstantStatusNotes as InstanttatusNotesSummary,
tGenie.GenieNotes,
tGenieType.GenieType,
tgenielabel.GenieLabel

from tcompany


inner join tsearch on tCompany.GUID = tSearch.CompanyGUID
left join tcompanylocation on tcompanylocation.guid= tcompany.LocationGUID
left join tSearchType on tsearchtype.GUID = tSearch.SearchTypeGUID
left join tSearchResult on tSearchResult.GUID = tsearch.SearchResultGUID
left join tPeople on tPeople.GUID = tsearch.PlacedGUID
left join tPosition on tPosition.GUID = tsearch.PositionGUID
left join tDepartment on tdepartment.GUID = tsearch.DepartmentGUID
left join tInstantStatus on tInstantStatus.SearchGUID = tSearch.guid
left join tInstantStatusType on tInstantStatusType.GUID = tInstantStatus.InstantStatusGUID 
left join tWorkbench on tWorkbench.SearchGUID=tsearch.GUID
left join tSearchCluendex on tSearchCluendex.CPSGUID=tsearch.GUID 
left join tpeopleEducation on tpeople.GUID = tpeopleEducation.PeopleGUID
left join tBusDev on tsearch.GUID = tbusdev.SearchGUID
left join tGenie on tbusdev.GUID = tgenie.BusDevGUID
left join tGenieLink on tGenie.GUID = tgenielink.GenieGUID
left join tGenieLabel on tgenielink.GenieLabelGUID = tGenieLabel.GUID
left join tGenieType on tgenie.GenieTypeGUID=tGenieType.GUID

奇怪的是,在系统57中定义了GenieType,只发现了4个。同样使用GenieLabel,在系统16中定义了只有6个。

我是SQL / TSQL的初学者,这个查询看起来很大......它看起来不错吗?或者,如果没有关于DB结构的更多信息,很难说清楚......

HMMM ....建议有不同的答案,但上面的内容似乎有效。

我为什么要使用:

inner join
inner join
left outer

VS

inner join
left join
left join

VS

join 
join
left join

????

更新2:

感谢@Daniel的帮助。我按照你的建议附上了图表。

我可以在Update 1的查询中看到我想要的内容

希望一些善良的灵魂可以提供帮助。

Databse Structure

3 个答案:

答案 0 :(得分:1)

您的查询在CROSS JOINtcompanytsearch之间执行tGenieType,然后尝试加入tGenie表中的相应记录。这意味着您的查询将返回tcompanytsearchtGenieType可能的所有组合的记录, ,无论它们之间是否存在任何关系 ,然后尝试使用您的条件将结果链接到tGenie记录,可能会再次将结果相乘。

当你说你想要“所有可能的条目”时,你想要实现什么目标?

通常情况下,您需要执行INNER JOINLEFT JOIN而不是CROSS JOIN。例如:

SELECT
  tSearch.Description,
  tCompany.CompanyName,
  tSearch.CompanyGUID,
  tGenie.GenieNotes,
  tGenieType.GenieType
FROM
  tCompany
  INNER JOIN tSearch ON tCompany.GUID = tSearch.CompanyGUID
  LEFT JOIN tGenie ON tSearch.GUID = tGenie.SearchGUID
  LEFT JOIN tGenieType ON tGenieType.GUID = tGenie.GenieTypeGUID

这将从每个tSearch中提取每个tCompany,根据其外键匹配它们,然后在执行之前尝试根据假设的GUID列与相应的tGenie记录匹配与tGenieType相同。

答案 1 :(得分:1)

我建议尝试这个。

Buffer recentWebSocketMessages = BufferUtils.synchronizedBuffer(new CircularFifoBuffer(1000));

你偶然偶然发现了交叉加入。此外,连接表必须通过键,因此请确保正确识别它们。有关联接的详细信息,请参阅codinghorror博客。

答案 2 :(得分:0)

您没有加入FROM中的其他表(在新版本的Sql Server中不再支持此语法)

应该是这样的:

select tsearch.description
,tcompany.CompanyName
,tsearch.CompanyGUID
,tgenie.GenieNotes
,tGenieType.GenieType
from tcompany
inner join tsearch on tcompany.XXX = tsearch.XXX
inner join tGenieType on tcompany.YYY = tsearch.YYY
left outer join tGenie on tgenie.GenieTypeGUID=tGenieType.GUID