在多种类型的日期范围之间进行搜索?

时间:2019-06-23 01:17:44

标签: php sql date

问题是我正在尝试在两种类型的日期(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);

1 个答案:

答案 0 :(得分:0)

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

const functions = require('firebase-functions');
const admin = require('firebase-admin');

exports.posted = functions.firestore.document('post/{pid}').onCreate((snap, context) => {
  console.log("posted");
});

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

以下是一些文档,以防您需要有关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'; 东西的详细信息:https://dev.mysql.com/doc/refman/8.0/en/comparison-operators.html#operator_between

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