-2

我有一个连接大约 5 个表并显示所有结果的查询。记录显示在 Web 应用程序上,它应该有大约 70000 个条目,但是当我执行此连接时,它仅在 MSSQL 2008 中显示 51000 个条目。我想知道如何操作连接函数来显示所有 70000 个条目,或者添加外部连接或内部连接?我对连接功能没有太多经验,希望有人编辑我的查询以显示这 5 个表中的所有条目。我知道这是专业程序员的快速解决方案,我提前感谢您的时间和精力!干杯

现有查询:

   select SerialNum as [Serial Number],ts_sitename As Site,(case m.Scratched
   when 0 then 'Live'
   when 1 then 'Free'
   END) as Status,  Note as Comment, 
   SUBSTRING(cast(EffectiveDate as char), 1, 8) AS [Effective Date Added], 
   SUBSTRING(cast(EffectiveDate as char), 10, 6) AS [Effective Time Added], 
   SUBSTRING(cast(ScratchedDate as char), 1, 8) AS [Scratched Date], 
   SUBSTRING(cast(ScratchedDate as char), 10, 6) AS [Scratched Time], 
   SUBSTRING(cast(ChangedPurgeDate as char), 1, 8) AS [Purge Date], 
   SUBSTRING(cast(ChangedPurgeDate as char), 10, 6) AS [Purge Time], 
   fl_filename as Dataset, hs_hostname,UserCode
   from (((((Media m join TheSites_tab s on m.SiteId = s.ts_sitenum) 
   join MediaGenT g on m.MediaId = g.mg_medianum) 
   join TheHosts_tab h on mg_hostnum = h.hs_hostnum)
   join TheFiles_tab f on mg_filenum = f.fl_filenum)
   join [User] u on mg_usernum = u.UserId)
   join Note n on m.NoteId = n.NoteId 
   where Destroyed = '0';
4

2 回答 2

0

尝试相同,但使用OUTER JOINon 而不是JOIN

select SerialNum as [Serial Number],ts_sitename As Site,(case m.Scratched
   when 0 then 'Live'
   when 1 then 'Free'
   END) as Status,  Note as Comment, 
   SUBSTRING(cast(EffectiveDate as char), 1, 8) AS [Effective Date Added], 
   SUBSTRING(cast(EffectiveDate as char), 10, 6) AS [Effective Time Added], 
   SUBSTRING(cast(ScratchedDate as char), 1, 8) AS [Scratched Date], 
   SUBSTRING(cast(ScratchedDate as char), 10, 6) AS [Scratched Time], 
   SUBSTRING(cast(ChangedPurgeDate as char), 1, 8) AS [Purge Date], 
   SUBSTRING(cast(ChangedPurgeDate as char), 10, 6) AS [Purge Time], 
   fl_filename as Dataset, hs_hostname,UserCode
   from (((((Media m join TheSites_tab s on m.SiteId = s.ts_sitenum) 
   OUTER JOIN MediaGenT g on m.MediaId = g.mg_medianum) 
   OUTER JOIN TheHosts_tab h on mg_hostnum = h.hs_hostnum)
   OUTER JOIN TheFiles_tab f on mg_filenum = f.fl_filenum)
   OUTER JOIN [User] u on mg_usernum = u.UserId)
   OUTER JOIN Note n on m.NoteId = n.NoteId 
   where Destroyed = '0';
于 2013-08-15T14:03:14.357 回答
0

JOIN单独使用的关键字默认为 a INNER JOIN,它基于相交进行连接。

我对您的数据集了解不多,但如果您排除了您认为不应该排除的数据,您应该使用更具包容性的联接。尝试用 s 替换您的JOIN关键字OUTER JOIN

W3 Schools是学习JOIN语义的好资源,可以找出最适合您需求的语义,还可以让您自己尝试不同类型的JOIN语句以获得更舒适的体验。

于 2013-08-15T14:08:59.917 回答