sql查询返回null
我使用完全相同的查询从两个表中获取数据。当我不使用sql查询返回null,sql,sql-server,sql-server-2005,Sql,Sql Server,Sql Server 2005,我使用完全相同的查询从两个表中获取数据。当我不使用c.IsReceived=0部分时,它返回日期,但在使用时不返回任何内容。对于RepID=205,在RepDailyCollection中有IsReceived=0的行。你认为我做错了什么?这是2005年的SQL Server。谢谢你 Select distinct RepDailyInfo.Date from RepDailyInfo left outer join RepDailyCollection c on
c.IsReceived=0
部分时,它返回日期,但在使用时不返回任何内容。对于RepID=205
,在RepDailyCollection
中有IsReceived=0
的行。你认为我做错了什么?这是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
如果您有“LEFT JOIN…ON…=RepDailyInfo.RepDailyInfoID”,那么应该是“LEFT JOIN…ON…=RepDailyInfo.RepDailyCollectionID”?它可能是一个varchar/char字段吗
c.IsReceived = '0'
RepDailyCollection
中可能存在RepID为205的记录,但如果在RepDailyInfo
中没有RepID为205的记录,则不会因为where子句而返回任何记录
根据你的陈述
RepDailyCollection中存在IsReceived=0表示RepID=205的行
听起来您的where子句应该是:
c.RepID = 205 and c.IsReceived = 0
而不是
RepDailyInfo.RepID = 205 and c.IsReceived = 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'
这里有很多错误 在第一个查询中,您有:
Select
distinct RepDailyInfo.Date
from
RepDailyInfo
left outer join
RepDailyCollection c
on
c.RepDailyCollectionID = RepDailyInfo.RepDailyInfoID
where
RepDailyInfo.RepID = 205 and c.IsReceived = 0
即使你说“left-outer-join”,由于where子句,你也可以说是inner-join
无论如何,此查询与第二个查询不匹配。在第二个查询中,您将在RepDailyInfoID上加入RepDailyCollection到RepDailyInfo。在第一个查询中,您加入了完全不同的列。谢谢,但我在RepDailyInfo中确实有行RepID=205,RepDailyCollection中没有列RepID。我反复检查:-(问题是否是因为加入??请查看问题的编辑部分?虽然我可以使用编辑部分获得结果,但我想知道我问的最初问题有什么问题。。感谢您的时间@abeIsReceived是真是假。:-(你能看一下问题Pratik的编辑部分吗?虽然我可以得到结果,但我想知道我问的最初问题有什么问题。我发布的帖子认为你只要求sql。请告诉我为什么你要将布尔类型值与0>进行比较?是在sql server中吗?>在oracle中,你可以直接检查布尔值w。)对于
true
或false
请查看编辑部分,它工作正常,并给出日期,但不是我问的真实问题。我很高兴你得到了答案。我只知道,当我加入的字段不匹配时,我往往不会得到任何结果。你是对的,克里斯。我把查询搞砸了-(我知道可能出了什么问题。谢谢你抽出时间!