sql查询返回null

时间:2012-03-28 18:48:03

标签: sql sql-server sql-server-2005

我使用完全相同的查询从两个表中获取数据。当我不使用c.IsReceived = 0部分时,它会返回日期,但在使用时它不会返回任何内容。 RepDailyCollection中有行IsReceived = 0 RepID = 205。你觉得我做错了什么?这是2005年的SQL Server。揍你!!

Select 
  distinct RepDailyInfo.Date 
from 
  RepDailyInfo 
left outer join 
  RepDailyCollection c 
on 
  c.RepDailyCollectionID = RepDailyInfo.RepDailyInfoID
where 
  RepDailyInfo.RepID = 205 and c.IsReceived = 0

编辑:

当我使用其他类似的存储过程工作正常。

Select i.Date
--i.RepDailyInfoID, i.RepID, i.TypeofDayID, i.CommuniTeeID, sum(cast(c.AmountSold as    numeric(10,2))) as AmountSold, 
--  sum(cast (c.AmountCollected as numeric(10,2))) as AmountCollected, c.NewCompanyName,c.PaymentMethod, 
--  c.TypeofCreditCard, c.CheckNumber, c.Invoice, c.Spots, TypeofDay.TypeofDay, CommuniTee.City, CommuniTee.State, 
--  CommuniTee.year, SalesRep_Info.FirstName, SalesRep_Info.LastName
from RepDailyInfo i
left outer join RepDailyCollection c on i.RepDailyInfoID = c.RepDailyInfoID 
left outer join TypeOfDay on TypeOfDay.TypeofDayID = i.TypeofDayID
left outer join SalesRep_Info on SalesRep_Info.RepID = i.RepID
left outer join CommuniTee on CommuniTee.CommuniTeeID = i.CommuniTeeID
 where i.RepID = 205 and c.IsReceived = 0 
 group by i.RepDailyInfoID, i.Date, i.TypeofDayID, i.CommuniTeeID, SalesRep_Info.FirstName, TypeofDay.TypeofDay, 
CommuniTee.City, CommuniTee.State, CommuniTee.year, SalesRep_Info.FirstName, 
SalesRep_Info.LastName, i.RepID, c.NewCompanyName, c.PaymentMethod, c.TypeofCreditCard, c.CheckNumber, c.Invoice, c.Spots
order by SalesRep_Info.FirstName desc

5 个答案:

答案 0 :(得分:0)

如果你有“LEFT JOIN ... ON ... = RepDailyInfo.RepDailyInfoID”,那实际上应该是“LEFT JOIN ... ON ... = RepDailyInfo.RepDailyCollectionID”吗?

答案 1 :(得分:0)

可能是varchar / char字段吗?

c.IsReceived = '0' 

答案 2 :(得分:0)

RepDailyCollection中可能有IsReceived = 0RepID = 205的记录,但如果RepDailyInfo中没有任何记录,且回复率为205,则不会有任何记录由于你的where子句而退回。

根据您的陈述

  

RepDailyCollection中有行,对于RepID = 205

,IsReceived = 0

听起来你的where子句应该是:

c.RepID = 205 and c.IsReceived = 0

而不是

RepDailyInfo.RepID = 205 and c.IsReceived = 0

答案 3 :(得分:0)

进行测试..您可以进行以下查询..

Select distinct c.IsReceived from RepDailyInfo 
left outer join RepDailyCollection c on c.RepDailyCollectionID = RepDailyInfo.RepDailyInfoID
where RepDailyInfo.RepID = 205 and c.IsReceived = 0

如果你看到0那么这个列必须是varchar2数据类型..

然后使用以下查询\

Select distinct  RepDailyInfo.Date from RepDailyInfo 
left outer join RepDailyCollection c on c.RepDailyCollectionID = RepDailyInfo.RepDailyInfoID
where RepDailyInfo.RepID = 205 and c.IsReceived = '0'

答案 4 :(得分:0)

这里有很多问题。

在第一个查询中,您有:

Select 
  distinct RepDailyInfo.Date 
from 
  RepDailyInfo 
left outer join 
  RepDailyCollection c 
on 
  c.RepDailyCollectionID = RepDailyInfo.RepDailyInfoID
where 
  RepDailyInfo.RepID = 205 and c.IsReceived = 0

即使您说“左外连接”,由于where子句,您也可以说内连接。

无论如何,此查询与第二个查询不匹配。在第二个查询中,您将在 RepDailyInfoID 上将RepDailyCollection加入RepDailyInfo。在第一个查询中,您将加入完全不同的列。