简体   繁体   中英

sql query return null

I was using the exact same query to get data from two tables. when i dont use the c.IsReceived = 0 part, it returns Dates but it doesn't return anything when it is used. There are rows in RepDailyCollection with IsReceived = 0 for RepID = 205 . What do you think i am doing wrong? This is in 2005 SQL Server. thnak you!!

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

Edit:

When i use an other stored proc similar to this works fine.

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' 

There might be records in RepDailyCollection with IsReceived = 0 and RepID = 205 , but if there aren't any records in RepDailyInfo with the repid of 205 then no records will be returned because of your where clause.

Based on your statement

There are rows in RepDailyCollection with IsReceived = 0 for RepID = 205

It sounds like your where clause should be:

c.RepID = 205 and c.IsReceived = 0

instead of

RepDailyInfo.RepID = 205 and c.IsReceived = 0

for your testing .. you can do following query..

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

if you can see 0 then this column must be varchar2 datatype ..

then use following query \\

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

There are a number of things wrong here.

In the first query you have:

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

Even though you say "left outer join", due to the where clause you may as well have said inner join.

Regardless, this query doesn't match the second one. In the second query you are joining RepDailyCollection to RepDailyInfo on RepDailyInfoID . In the first query you are joining on completely different columns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM