【问题标题】:Search between multiple type of date range?在多种日期范围之间搜索?
【发布时间】:2019-11-05 07:15:27
【问题描述】:

问题是我试图在 2 种日期(date_open 和 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">

我的日期输入是这样的

开放日期:2016-01-11 致:2016-02-12 截止日期:2018-02-23 致:2018-03-26

假设返回这样的列表

 +---------+------------------+-------+-----------+------------+------------+
 | 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   |
 +---------+------------------+-------+-----------+------------+------------+

但它根本不返回任何值,也不会弹出错误

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

我的搜索代码是这样的

$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);

【问题讨论】:

  • 你需要学习如何使用参数而不是修改查询字符串。

标签: php sql date


【解决方案1】:

我相信您正在寻找的 SQL 是这样的:

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';

这里是工作示例:http://sqlfiddle.com/#!9/d35d84/4

如果您需要有关 between ... and thingie 的详细信息,这里有一些文档:https://dev.mysql.com/doc/refman/8.0/en/comparison-operators.html#operator_between

顺便说一句,我希望您插入到 SQL 语句中的变量被正确过滤,否则这将是 SQL 注入的好地方。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多