2012-03-28 232 views
0

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

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 

回答

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子句你不妨说内连接。

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

+0

你是对的Chris。我搞砸了查询。 :-(我明白可能出错了,谢谢你的时间! – Ram 2012-03-28 19:32:58

0

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

+0

请参阅编辑部分,它工作正常,并给我日期,但不是我问的真正的问题.. – Ram 2012-03-28 19:12:45

+0

我很高兴你有你的答案。我只知道当我与我加入的字段不匹配时,我往往不会得到任何结果。 – Brian 2012-03-29 16:25:29

0

难道它是一个varchar/char字段?

c.IsReceived = '0' 
+0

不,这是一个布尔值:-( – Ram 2012-03-28 18:53:24

0

有可能在RepDailyCollection记录与IsReceived = 0RepID = 205,但如果没有在RepDailyInfo任何记录与205 REPID则没有记录会因为你的where子句中返回。

根据您的陈述与RepDailyCollection

有行IsReceived = 0 REPID = 205

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

c.RepID = 205 and c.IsReceived = 0 

代替的

RepDailyInfo.RepID = 205 and c.IsReceived = 0 
+0

谢谢,但我在RepDailyInfo行中有RepID = 205 – Ram 2012-03-28 18:54:46

+0

RepDailyCollection中没有列RepID我双重检查:-(问题是因为连接? – Ram 2012-03-28 19:05:35

+0

请看这个问题的编辑部分?虽然我可以使用编辑的部分得到结果,但我想知道我问的最初问题有什么问题..感谢您的时间@abe – Ram 2012-03-28 19:14:42

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' 
+0

IsReceived是真或假。 :-( – Ram 2012-03-28 19:06:36

+0

你可以看看Pratik的问题的编辑部分吗?虽然我可以得到结果,但我想知道我问的最初问题有什么问题。 – Ram 2012-03-28 19:13:45

+0

我曾经发布过这样的看法,认为你只是要求sql。 请告诉我为什么你要比较一个布尔类型的值为0>? 它是在SQL服务器..> 在oracle中,你可以直接用'true'或'false'来检查布尔值 – 2012-03-28 20:21:49