简体   繁体   中英

MySQL between dates selecting Null values

I have an Sp to select some values from a table and there is a left outer join, and the condition of the join is to select with respect to from and to dates.

I need to select data b/w these two dates.

this is how the part looks like

SELECT *// all the needed columns
 FROM  mytable            
     //other joins   
     LEFT OUTER JOIN myview ON              
        //some conditions         
        myview .soldDate between @fromdate and @todate    

The selection works fine, it selecting only data b/w those dates, but also selcting null dates.

在此处输入图片说明

How do I avoid selecting these null values?

You can try to let condition in where instead of on , because you are using outer join

SELECT *// all the needed columns
 FROM  mytable            
     //other joins   
     LEFT OUTER JOIN myview ON              
        //some conditions         
WHERE 
    myview.soldDate between @fromdate and @todate   

Or just use INNER JOIN instead of LEFT OUTER JOIN

Exclude rows in join that does does not have a soldDate

SELECT * -- all the needed columns
FROM mytable
--other joins
LEFT OUTER JOIN myview ON -- conditions
        (myview.soldDate IS NOT NULL AND myview.soldDate between @fromdate and @todate

Your question doesn't explain why soldDate is NULL . It could be because of LEFT JOIN , but also by design.

If it's the first, just use a INNER JOIN. If it's by design, use my query above.

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