我正在尝试仅显示从当前日期起提前两周的记录。 starttime作为datetime数据类型存储在数据库中。

我有这个,

       SELECT id, date_format(starttime, '%d-%m-%Y %H:%i') AS formatted_start, date_format(starttime, '%Y-%m-%d') AS formatted_date,
    date_format(endtime, ' %H:%i') AS formatted_end
    FROM timedates WHERE user_id = 1 AND `status`='' AND  YEARWEEK(formatted_date, 0) IN (YEARWEEK(NOW(), 0),
YEARWEEK(DATE_ADD(NOW(), INTERVAL 2 WEEK), 0))

但是我收到语法错误YEARWEEK(formatted_date, 0) IN (YEARWEEK(NOW(), 0) AND YEARWEEK(DATE_ADD(NOW()
谁能告诉我这是怎么回事?

最佳答案

正如我所说,我认为没有理由使用YEARWEEK函数。您需要将开始日期设置为今天,即CURDATE(),再加上2个星期,即DATE_ADD(CURDATE(), INTERVAL 2 WEEK),然后我们只检查这两个日期之间的starttime

SELECT id, date_format(starttime, '%d-%m-%Y %H:%i') AS formatted_start,
    date_format(starttime, '%Y-%m-%d') AS formatted_date,
    date_format(endtime, ' %H:%i') AS formatted_end
FROM timedates
WHERE user_id = 1
  AND `status`=''
  AND  DATE(starttime) BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 2 WEEK)

10-06 15:15