dongtiran7769 2015-03-22 14:01
浏览 59
已采纳

从mysql查询中提取特定日期

I have to columns in my table (check_in and checkout). Both of them has this format dd-mm-yyyy. I want to check if there is any date available between my two paramaters(inputs).

I tried like this:

select * from rezervare where date(check_out) > date('my_checkout_input') 

but this works only in a few cases. If i have a reservation between 12-03-2015 - 15-03-2015 and another reservation between 17-03-2015 and 20-03-2015. It will not take the date between 16-03-2015 as a free date. Can you give me some ideas ?

  • 写回答

3条回答 默认 最新

  • dpwjx32578146 2015-03-22 18:16
    关注
    create table rezervare (
        id int auto_increment,
        check_in varchar(10),
        checkout varchar(10),
        primary key (id)
    );
    insert into rezervare values
        (null, '12-03-2015', '12-03-2015'),
        (null, '13-03-2015', '13-03-2015'),
        (null, '14-03-2015', '14-03-2015'),
        (null, '15-03-2015', '15-03-2015'),
        (null, '16-03-2015', '16-03-2015'),
        (null, '17-03-2015', '17-03-2015'),
        (null, '19-03-2015', '19-03-2015'),
        (null, '20-03-2015', '20-03-2015')
    ;
    
    select * from rezervare;
    +----+------------+------------+
    | id | check_in   | checkout   |
    +----+------------+------------+
    |  1 | 12-03-2015 | 12-03-2015 |
    |  2 | 13-03-2015 | 13-03-2015 |
    |  3 | 14-03-2015 | 14-03-2015 |
    |  4 | 15-03-2015 | 15-03-2015 |
    |  5 | 16-03-2015 | 16-03-2015 |
    |  6 | 17-03-2015 | 17-03-2015 |
    |  7 | 19-03-2015 | 19-03-2015 |
    |  8 | 20-03-2015 | 20-03-2015 |
    +----+------------+------------+
    

    If your schema same as this, you can't use date function, because:

    select id, date(check_in), date(checkout) from rezervare ;
    +----+----------------+----------------+
    | id | date(check_in) | date(checkout) |
    +----+----------------+----------------+
    |  1 | NULL           | NULL           |
    |  2 | NULL           | NULL           |
    |  3 | NULL           | NULL           |
    |  4 | NULL           | NULL           |
    |  5 | NULL           | NULL           |
    |  6 | NULL           | NULL           |
    |  7 | NULL           | NULL           |
    |  8 | NULL           | NULL           |
    +----+----------------+----------------+
    

    But you can use between, in my example it works fine:

    select * from rezervare where checkout between '12-03-2015' and '15-03-2015';
    +----+------------+------------+
    | id | check_in   | checkout   |
    +----+------------+------------+
    |  1 | 12-03-2015 | 12-03-2015 |
    |  2 | 13-03-2015 | 13-03-2015 |
    |  3 | 14-03-2015 | 14-03-2015 |
    |  4 | 15-03-2015 | 15-03-2015 |
    +----+------------+------------+
    
    select * from rezervare where checkout > '14-03-2015';
    +----+------------+------------+
    | id | check_in   | checkout   |
    +----+------------+------------+
    |  4 | 15-03-2015 | 15-03-2015 |
    |  5 | 16-03-2015 | 16-03-2015 |
    |  6 | 17-03-2015 | 17-03-2015 |
    |  7 | 19-03-2015 | 19-03-2015 |
    |  8 | 20-03-2015 | 20-03-2015 |
    +----+------------+------------+
    

    But true way - alter table and use correct date type.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 软件供应链安全是跟可靠性有关还是跟安全性有关?
  • ¥15 电脑蓝屏logfilessrtsrttrail问题
  • ¥20 关于wordpress建站遇到的问题!(语言-php)(相关搜索:云服务器)
  • ¥15 【求职】怎么找到一个周围人素质都很高不会欺负他人,并且未来月薪能够达到一万以上(技术岗)的工作?希望可以收到写有具体,可靠,已经实践过了的路径的回答?
  • ¥15 Java+vue部署版本反编译
  • ¥100 对反编译和ai熟悉的开发者。
  • ¥15 带序列特征的多输出预测模型
  • ¥15 Python 如何安装 distutils模块
  • ¥15 关于#网络#的问题:网络是从楼上引一根网线下来,接了2台傻瓜交换机,也更换了ip还是不行
  • ¥15 资源泄露软件闪退怎么解决?