简体   繁体   English

MYSQL SELECT之间如何做一个日期?

[英]MYSQL how to do a date between SELECT?

I have following query which basically all i want to do is to select the end_date between today and +3 days in the future, however my query gives me non-sense results including dates in the future for even 3 months ? 我有以下查询,基本上我想做的就是选择从今天到将来+3天之间的end_date,但是我的查询给了我无意义的结果,包括将来甚至3个月的日期? what i am i doing wrong here ? 我在这里做错了什么? i tried the date between also but didnt work too 我也尝试过之间的日期,但也没有工作

SELECT DATE_ADD( ccs.end_date, INTERVAL +3
DAY ) , DATE_FORMAT( NOW( ) , '%Y-%m-%d' ) , ccs. * , cc. *
FROM `customers_closure` cc, `customers_closure_service` ccs
WHERE cc.queue_id = '1'
AND ccs.closure_id = cc.id
AND (
ccs.end_date >= DATE_FORMAT( NOW( ) , '%Y-%m-%d' )
AND ccs.end_date <= DATE_ADD( ccs.end_date, INTERVAL +3
DAY )
)
ORDER BY cc.id DESC
LIMIT 0 , 30

I got it sorted 我把它整理好了

SELECT ccs . * , cc . *
                    FROM `customers_closure` cc, `customers_closure_service` ccs
                    WHERE cc.queue_id = '1'
                    AND ccs.closure_id = cc.id
                    AND 
                    (
                    ccs.end_date BETWEEN CURDATE() + INTERVAL +3 DAY AND CURDATE()
                    )

                    ORDER BY cc.id DESC
SELECT DATE(ccs.end_date), ccs.*, cc. *
FROM `customers_closure` cc, `customers_closure_service` ccs
WHERE cc.queue_id = 1
AND ccs.closure_id = cc.id
AND DATE(ccs.end_date) BETWEEN DATE(NOW()) AND DATE( DATE_ADD(NOW(), INTERVAL +3 DAY) )
ORDER BY cc.id DESC
LIMIT 0 , 30

Give this a try. 试试看。

Are your end_date values DATETIMEs? 您的end_date值是DATETIMEs吗? If so you'd best avoid BETWEEN clauses. 如果是这样,您最好避免使用BETWEEN子句。

Try this instead. 试试这个吧。

... ccs.end_date >= CURDATE()
AND ccs.end_date <  CURDATE() + INTERVAL 4 DAY

You do that (with the end of the range selected by a < instead of a <= and the range extended by a day for this reason: the end of your BETWEEN range leaves ou all the times of the end date except midnight. 您可以这样做(用<而不是<=来选择范围的结尾,并且由于这个原因将范围延长了一天:因为您BETWEEN范围的结尾离开了结束日期的所有时间,除了午夜。

Just add +1 day to the wanted days you want to retrieve the data's. 只需将+1天添加到要检索数据的所需天数即可。 Like the code below: 像下面的代码:

SELECT DATE_ADD( ccs.end_date, INTERVAL +4
DAY ) , DATE_FORMAT( NOW( ) , '%Y-%m-%d' ) , ccs. * , cc. *
FROM `customers_closure` cc, `customers_closure_service` ccs
WHERE cc.queue_id = '1'
AND ccs.closure_id = cc.id
AND (
ccs.end_date >= DATE_FORMAT( NOW( ) , '%Y-%m-%d' )
AND  ccs.end_date < DATE_ADD( NOW( ), INTERVAL + 4
DAY )
)
ORDER BY cc.id DESC
LIMIT 0 , 30

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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