简体   繁体   中英

how to write a where clause in sql query where I need to compare date field with current time?

I have three variables 1. Start Date (column in a table) 2. Current Date 3. Start Date + 1 week

I want to write a where clause in sql query in such a way that

Current Date should be between Start Date and (Start Date + 1 week) and Current Date can be less than Start Date but not greater than (Start Date + 1 week )

使用between函数:

 where now() between startDate and endDate

The logic of

Current Date should be between Start Date and (Start Date + 1 week)

and

Current Date can be less than Start Date but not greater than (Start Date + 1 week )

is conflicting, since if currentDate should be between startDate and startDate+ 1week, it will never be less than startDate

If you mean currentDate should be between startDate and startDate+1week write

WHERE DATE(NOW()) BETWEEN `startDate` AND DATE_ADD(`startDate`, INTERVAL 1 WEEK)

If you mean currentDate should be less than startDate+1week write

WHERE DATE(NOW()) <= DATE_ADD(`startDate`, INTERVAL 1 WEEK)

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