dsljpwi494719 2019-06-23 01:17
浏览 183

在多种日期范围之间搜索?

The problem is I'm trying to search between records between 2 type of date which is (date_open and date_close)

 +---------+------------------+-------+-----------+------------+------------+
 | File ID | File Desc.       |Return | Open Date | Close Date | Open/Close |
 +---------+------------------+-------+-----------+------------+------------+
 | 400/4   | 1 - Test 1 400/4 |       |2016-02-12 | 2018-03-26 |    Close   |
 +---------+------------------+-------+-----------+------------+------------+
 | 400/1   | 5 - Test 5 400/1 |       |2016-01-11 | 2018-02-23 |    Close   |
 +---------+------------------+-------+-----------+------------+------------+
 | 400/1   | 2 - Test 2 400/1 |       |2015-03-16 | 2017-05-20 |    Close   |
 +---------+------------------+-------+-----------+------------+------------+
 | 400/2   | 3 - Test 3 400/2 |       |2015-05-15 | 2017-02-11 |    Close   |
 +---------+------------------+-------+-----------+------------+------------+
<label>File ID</label>
        <input name="txtKeyword" type="text" id="txtKeyword" value="<?php echo $strKeyword;?>" placeholder="Ex. 400/1">
<label>File Description</label>
        <input name="txtKeyword2" type="text" id="txtKeyword2" value="<?php echo $strKeyword2;?>" placeholder="Ex. 10 - Pelbagai Surat Menyurat Ansuran Cukai Tanah">
<label>Date Open from </label>
        <input name="txtKeyword3" type="date" id="txtKeyword3" value="<?php echo $strKeyword3;?>">
<label>To </label>
        <input name="txtKeyword4" type="date" id="txtKeyword4" value="<?php echo $strKeyword4;?>">
      <br />
<label>Date Close from </label>
        <input name="txtKeyword5" type="date" id="txtKeyword5" value="<?php echo $strKeyword5;?>">
<label>To </label>
        <input name="txtKeyword6" type="date" id="txtKeyword6" value="<?php echo $strKeyword6;?>">
<br />
<label>Status</label>
        <input name="txtKeyword7" type="text" id="txtKeyword7" value="<?php echo $strKeyword7;?>">
<br />
<input class="search" type="submit" value="Search">

My date inputs are like this

Date Open from: 2016-01-11 To: 2016-02-12 Date Close from: 2018-02-23 To: 2018-03-26

It suppose to return list like this

 +---------+------------------+-------+-----------+------------+------------+
 | File ID | File Desc.       |Return | Open Date | Close Date | Open/Close |
 +---------+------------------+-------+-----------+------------+------------+
 | 400/4   | 1 - Test 1 400/4 |       |2016-02-12 | 2018-03-26 |    Close   |
 +---------+------------------+-------+-----------+------------+------------+
 | 400/1   | 5 - Test 5 400/1 |       |2016-01-11 | 2018-02-23 |    Close   |
 +---------+------------------+-------+-----------+------------+------------+

But it returns no value at all with no error pop up

 +---------+------------------+-------+-----------+------------+------------+
 | File ID | File Desc.       |Return | Open Date | Close Date | Open/Close |
 +---------+------------------+-------+-----------+------------+------------+

My searching code are like this

$sql = "SELECT * FROM file_list WHERE
              CONCAT(`file_no`) LIKE '%".$strKeyword."%'
          AND CONCAT(`file_desc`) LIKE '%".$strKeyword2."%'
          AND CONCAT(`date_open`) LIKE ((CONCAT(`date_open`) >= '%".$strKeyword3."%' AND  CONCAT(`date_close`) <= '%".$strKeyword4."%'))
          AND CONCAT(`date_close`) LIKE ((CONCAT(`date_close`) >= '%".$strKeyword5."%' AND CONCAT(`date_close`) <= '%".$strKeyword6."%'))
          AND CONCAT(`open_close`) LIKE '%".$strKeyword7."%'";

    $query = mysqli_query($dbConn,$sql);
  • 写回答

1条回答 默认 最新

  • dtvhqlc57127 2019-06-23 01:25
    关注

    I belive SQL you're looking for is this:

    select *
    from file_list
    where open_date between '2016-01-11' and '2016-02-12'
      and close_date between '2018-02-23' and '2018-03-26';
    

    And here is working example: http://sqlfiddle.com/#!9/d35d84/4

    And here are some docs in case you need details on between ... and thingie: https://dev.mysql.com/doc/refman/8.0/en/comparison-operators.html#operator_between

    BTW, I hope variables you're inserting into SQL statement are properly filtered, otherwise that would be a good place for SQL injection.

    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题