简体   繁体   中英

how to check given date exist in table or not?

in my database table contains

id  Start_date End_date      plans
-- ---------   ----------  --------
1  0000-00-00  2015-12-31  classic
2  2016-01-01  0000-00-00  Mini

if i give Start_date ="2016-02-15" End_date ="2016-04-30" then my output should be "2 2016-01-01 0000-00-00 Mini

in case if i give Start_date ="2015-06-01" End_date ="2016-04-30"
then my output should be
1  0000-00-00  2015-12-31  classic and 2  2016-01-01  0000-00-00  Mini
previously my query was 
SELECT * FROM plantb WHERE CusID='1' AND AccID='1001' AND SerID='0123456789' AND ( Start_date BETWEEN '2015-06-01' AND '2016-04-30' ) OR ( End_date BETWEEN '2015-06-01' and '2016-04-30') ORDER BY `Start` ASC

i think you just trying to get plans now valid or not by start date end date .

try this

SELECT * 
FROM plantb 
WHERE CusID='1' 
      AND AccID='1001' 
      AND SerID='0123456789' 
      AND CURDATE() BETWEEN  Start  and  End   
ORDER BY `Start` ASC
SELECT * FROM `plantb` WHERE `CusID` = 1 AND `AccID` = 1001 AND `SerID` = 0123456789 
AND 
    ((SELECT CASE `Start_date` WHEN '0000-00-00' THEN (`End_date` >= '2015-06-01') ELSE ('2015-06-01' BETWEEN `Start_date` AND `End_date`) END ) OR (SELECT CASE `End_date` WHEN '0000-00-00' THEN (`Start_date` <= '2016-04-30') ELSE ('2016-04-30' BETWEEN `Start_date` AND `End_date`) END ))

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