【问题标题】:select data from mysql using two column ranges使用两列范围从 mysql 中选择数据
【发布时间】:2016-04-09 02:02:20
【问题描述】:

我有这样的数据

sno name    date        time

1   Irona   2016-01-01  10:00:00
2   Meona   2016-01-02  21:00:00
3   Lessa   2016-01-02  8:00:00
4   Airik   2016-01-03  10:00:00

我正在尝试这样的查询

SELECT * FROM `appointment` where (date <= '2016-01-02' and time >= '21:00:00') and (date >= '2016-01-03' and time >= '10:00:00')

我想要那些在 2016 年 1 月 2 日到 2016 年 1 月 3 日之间以及晚上 9 点到上午 10 点之间的约会

【问题讨论】:

  • 你遇到了什么错误?日期和日期列的类型有哪些?

标签: mysql sql datetime select str-to-date


【解决方案1】:

试试这个:

SELECT * 
FROM `appointment` A
WHERE STR_TO_DATE(CONCAT(A.date, ' ', A.time), '%Y-%m-%d %H:%i:%s') BETWEEN '2016-01-02 21:00:00' AND '2016-01-03 10:00:00'

【讨论】:

    【解决方案2】:

    查询 date &lt;= '2016-01-02' and date &gt;= '2016-01-03' 不返回任何内容。 你应该把它改成date &gt;= '2016-01-02' and date &lt;= '2016-01-03'

    SELECT * FROM `appointment` 
    where date >= '2016-01-02' and date <= '2016-01-03' 
    and time <= '21:00:00' and time >= '10:00:00'
    

    【讨论】:

      【解决方案3】:

      我会这样做:

      ...
      WHERE
      to_char(date, '%Y%m%d') between '20160102' and '20160103'
      AND to_char(timedate, '%H') between '10' and '21'
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-12-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-13
        • 2013-11-23
        • 1970-01-01
        • 2016-07-07
        相关资源
        最近更新 更多