查看显示不正确的数据

时间:2013-11-19 23:05:47

标签: sql-server sql-server-2008-r2 sql-view

我有一个非常简单的数据库。它包含3个表。第一个是值输入的主输入表。第二个和第三个纯粹用于将值转换为视图的名称。当我查看表1中的行时,我可以看到列businessUnit包含所有行中的有效值。当我添加Business_Units表(此DB中的第3个表)时,除了2行之外的所有行都会消失,尽管第1个表中的businessUnit值都是1,但视图会为它们指定不同的名称。

我创建了一个数据库图并上传了一个截图到imgur。链接:http://imgur.com/jXF7L1R

enter image description here

我在表格中只有2个关系。一个来自New_Equipment_User上的equipType到Equipment_Type中的id,另一个来自New_Equipment_User中的businessUnit到Business_Units中的id。奇怪的是,设备类型完美地工作,但是当我完全复制表,关系和视图信息时,它不起作用。而不是出现6行,只有2行在businessUnit中共享相同的值,但为它提供了2个不同的名称。

如果重要,这是我的观点查询:

SELECT dbo.New_Equipment_User.id, dbo.Equipment_Type.name AS equipType, dbo.New_Equipment_User.jobNumber,     dbo.New_Equipment_User.costCode, 
  dbo.New_Equipment_User.reason, dbo.New_Equipment_User.mobile, dbo.New_Equipment_User.mobileQty, dbo.New_Equipment_User.mobileComment, 
  dbo.New_Equipment_User.laptop, dbo.New_Equipment_User.laptopQty, dbo.New_Equipment_User.laptopComment, dbo.New_Equipment_User.desktop, 
  dbo.New_Equipment_User.desktopQty, dbo.New_Equipment_User.desktopComment, dbo.New_Equipment_User.modem, dbo.New_Equipment_User.modemQty, 
  dbo.New_Equipment_User.modemComment, dbo.New_Equipment_User.printer, dbo.New_Equipment_User.printerQty, dbo.New_Equipment_User.printerComment, 
  dbo.New_Equipment_User.camera, dbo.New_Equipment_User.cameraQty, dbo.New_Equipment_User.cameraComment, dbo.New_Equipment_User.dateRequired, 
  dbo.New_Equipment_User.requestedBy, dbo.New_Equipment_User.dateRequested, dbo.New_Equipment_User.approvalStatus, 
  dbo.Business_Units.name AS businessUnit
FROM dbo.New_Equipment_User
     JOIN dbo.Equipment_Type ON dbo.New_Equipment_User.equipType = dbo.Equipment_Type.id
     JOIN dbo.Business_Units ON dbo.New_Equipment_User.id = dbo.Business_Units.id
WHERE  (dbo.New_Equipment_User.approvalStatus = '0')

这是视图的图像,因为它更容易阅读:http://imgur.com/pZ97ehQ

enter image description here

是否有人能够协助解决这可能发生的原因?

1 个答案:

答案 0 :(得分:1)

尝试使用LEFT JOIN

SELECT ...

FROM dbo.New_Equipment_User
     JOIN dbo.Equipment_Type ON dbo.New_Equipment_User.equipType = dbo.Equipment_Type.id
     LEFT JOIN dbo.Business_Units ON dbo.New_Equipment_User.id = dbo.Business_Units.id

这将确保所有dbo.New_Equipment_User和所有dbo.Equipment_Type都存在