如何在sql中获取2个日期

时间:2016-11-17 05:54:48

标签: mysql sql date

我想减去2个日期,以比较当前年份与上一年度的数据,但我的查询无效。

如果我删除了一段时间,但不确定如何拉2个句号。

由于

 SELECT  year(date) as year, WEEK(ADDDATE(date, 5-DAYOFWEEK(date)), 3) AS 'Week', DATE, 
 FROM TABLE
 WHERE year(date) in (2015, 2016)
 AND (DATE BETWEEN "2016-10-23" AND "2016-11-12")
 AND (DATE BETWEEN "2015-11-06" AND "2015-11-12")
 ORDER BY DATE, year(date)

3 个答案:

答案 0 :(得分:0)

请试试这个。

 SELECT  year(date) as year, WEEK(ADDDATE(date, 5-DAYOFWEEK(date)), 3) AS 'Week', DATE, 
 FROM TABLE
 WHERE 
 ((DATE BETWEEN "2016-10-23" AND "2016-11-12")) OR 
 ((DATE BETWEEN "2015-11-06" AND "2015-11-12"))
 ORDER BY DATE, year(date)

答案 1 :(得分:0)

我想你想要or

SELECT year(date) as year, WEEK(ADDDATE(date, 5-DAYOFWEEK(date)), 3) AS Week, DATE
FROM TABLE
WHERE DATE BETWEEN '2016-10-23' AND '2016-11-12' OR
      DATE BETWEEN '2015-11-06' AND '2015-11-12';

注意:

  • 您不需要date()子句中的where比较。
  • 您应该对日期和字符串常量使用单引号。
  • 您不应对列别名使用单引号。

答案 2 :(得分:0)

SELECT year(DATE) AS year
,WEEK(ADDDATE(DATE, 5 - DAYOFWEEK(DATE)), 3) AS 'Week'
,DATE
FROM TABLE
WHERE (
    (
        DATE BETWEEN '2016-10-23'
            AND '2016-11-12'
        )
    OR (
        DATE BETWEEN '2015-11-06'
            AND '2015-11-12'
        )
    )