【问题标题】:date query between two dates两个日期之间的日期查询
【发布时间】:2012-12-21 06:21:43
【问题描述】:

在我的表中我有这个架构

record      startDate    endDate
abs         2005-11-29   2005-11-21
absasd          2005-11-23   2005-01-22
absty           2005-10-26   2005-02-23
absfx           2005-09-27   2005-09-24
absft           2005-10-28   2005-07-25
absyh           2005-01-28   2005-08-01
absdx           2005-12-29   2005-01-02
abs345          2005-06-24   2005-02-03
abser           2005-07-14   2005-04-06
absbv       2005-08-01   2005-05-08
abse        2005-07-12   2005-06-09
abserr          2005-12-30   2005-07-14
absf        2005-01-12   2005-12-16
abscv           2005-04-11   2005-12-26
abscv           2005-06-01   2005-11-27
absc        2005-07-03   2005-10-28
absv        2005-07-06   2005-09-25
.....
.....
......

我想选择 2005-01-01 和 2005-10-01 之间的所有记录。和某些其他日期。那将是什么 sql。

【问题讨论】:

  • “和某些其他日期”是什么意思?
  • 请注意其他日期,如果我对此有疑问,我会做剩下的。谢谢
  • 记录必须完全在您指定的日期范围内,还是只能部分与您指定的日期范围重叠?
  • 如果您有可能切换到 postgresql,您可能需要查看the following thread

标签: php mysql cakephp-1.3


【解决方案1】:
SELECT *
    FROM YourTable
    WHERE startDate >= '2005-01-01'
        AND endDate <= '2005-10-01'

【讨论】:

    【解决方案2】:

    拥有 1K+ 的声誉和这个问题?你还好吗? :-)

    SELECT * FROM TableName
    WHERE startDate > '2005-01-01' and startDate < '2005-10-01'
    --WHERE startDate > '2005-01-01' and endDate < '2005-10-01'
    --WHERE endDate > '2005-01-01' and endDate < '2005-10-01'
    --WHERE endDate > '2005-01-01' and startDate < '2005-10-01'
    --WHERE (startDate > '2005-01-01' and startDate < '2005-10-01' ) OR (endDate > '2005-01-01' and endDate < '2005-10-01')
    --WHERE (startDate > '2005-01-01' and startDate < '2005-10-01' ) AND (endDate > '2005-01-01' and endDate < '2005-10-01')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-25
      • 2014-11-22
      • 2020-07-19
      • 1970-01-01
      • 2023-04-06
      • 2020-04-17
      • 1970-01-01
      相关资源
      最近更新 更多