简体   繁体   中英

MySQL : first day of the week and last day of the week not working as desired

I have a table named stockins with a column stock . In that I want to find max(stock) between first date of the week and last date of the week.

To do that I am using the following query

select max(stock) 
from stockins 
where date >= '(DATE(NOW()) - INTERVAL (WEEKDAY(DATE(NOW()))) DAY)' 
and date <= '(DATE(NOW() + INTERVAL (6 - WEEKDAY(NOW())) DAY))'

But the output is coming wrong.

If I run the below query

select max(stock) 
from stockins 
where date >= '(DATE(NOW()) - INTERVAL (WEEKDAY(DATE(NOW()))) DAY)' 
and date <= '2018-01-28'

Then I am getting the desired output.

Where I could be going wrong?

只需删除单引号。

select max(stock) from stockins where date >= (DATE(NOW()) - INTERVAL (WEEKDAY(DATE(NOW()))) DAY) and date <= (DATE(NOW() + INTERVAL (6 - WEEKDAY(NOW())) DAY))

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