1

我目前在一个表单中有多个搜索字段,用户可以选择并选择要过滤的一个,在我添加日期范围之前一切正常。如果我单独添加日期范围而没有任何其他字段来过滤它,那么我知道这并不是我写错了查询。请帮我解决这个问题。谢谢!

这是有效的查询:(由于某种原因,SELECT * 不起作用,我必须输入我要显示的所有字段的名称)

它包括包含日期范围的表单部分。

<li class="form-line form-line-column" id="id_29">
    <label class="form-label-top" id="label_29" for="input_30">
        From Date:
    </label>
    <div id="cid_29" class="form-input-wide">
        <input type="text" class="form-textbox validate" id="datepicker" name="from_date" size="10" />
    </div>
</li>
<li class="form-line form-line-column" id="id_29">
    <label class="form-label-top" id="label_29" for="input_8">
        To Date:
    </label>
    <div id="cid_29" class="form-input-wide">
        <input type="text" class="form-textbox validate" id="datepicker2" name="to_date" size="10" />
    </div>
</li> 

<?php
    $sso = $_GET['sso'];
    $assigned_to = $_GET['assigned_to'];
    $case_status = $_GET['case_status'];
    $startdate = $_GET['from_date'];
    $enddate = $_GET['to_date'];
    $subject = $_GET['subject'];

    $sql = ("SELECT 
                id, sso, full_name, case_status, assigned_to, resolution, 
                description, updated, created, additional_notes, subject
            FROM rmstable2
            WHERE 
                sso LIKE '%$sso%' AND
                case_status LIKE '%$case_status%' AND
                assigned_to LIKE '%$assigned_to%' AND
                subject LIKE '%$subject%'");
?>

这是我想要的查询:

<?php
    $sql = ("SELECT 
                id, sso, full_name, case_status, assigned_to, resolution, 
                description, updated, created, additional_notes, subject
             FROM rmstable2
             WHERE 
                sso LIKE '%$sso%' AND
                case_status LIKE '%$case_status%' AND
                assigned_to LIKE '%$assigned_to%' AND
                subject LIKE '%$subject%'
                AND created >= '$startdate'
                AND created <= '$enddate'");
?>
4

4 回答 4

2

使用BETWEEN它,不要忘记用DATE( )

$sql = ("SELECT id, sso, full_name, case_status, assigned_to, 
                resolution, description, updated, created, 
                additional_notes, subject 
        FROM  rmstable2 
        WHERE sso LIKE '%$sso%' AND 
              case_status LIKE '%$case_status%' AND 
              assigned_to LIKE '%$assigned_to%' AND 
              subject LIKE '%$subject%' AND 
              DATE(created) BETWEEN DATE('$startdate') AND DATE('$enddate')");

更新 1

换成怎么ANDOR

$sql = ("SELECT id, sso, full_name, case_status, assigned_to, 
                resolution, description, updated, created, 
                additional_notes, subject 
        FROM  rmstable2 
        WHERE sso LIKE '%$sso%' OR 
              case_status LIKE '%$case_status%' OR
              assigned_to LIKE '%$assigned_to%' OR 
              subject LIKE '%$subject%' OR
              (DATE(created) BETWEEN DATE('$startdate') AND DATE('$enddate'))");
于 2012-08-08T14:42:12.197 回答
0

最佳实践是 echo $sql; 在 phpmyadmin 中使用并粘贴生成的查询(如果你有的话)!其他有用的命令是echo mysql_error().

在您的情况下,您很可能必须检查日期格式是否正确(应该是 YYYY-MM-DD)

也做这个 AND created >= '$startdate' AND created <= '$enddate'");

对此 AND created >= '" . $startdate . "' AND created <= '" . $enddate . "'");

因为如果...

$a=1;
echo '$a';

将输出

$a
于 2012-08-08T14:40:22.303 回答
0

假设您的值格式正确,您可以尝试在最终查询中使用 DATE(created),例如:

AND DATE(created) >= '$startdate' AND DATE(created) <= '$enddate'

甚至使用 BETWEEN 例如:

AND DATE(created) BETWEEN '$startdate' AND '$enddate'
于 2012-08-08T14:44:02.213 回答
0
// assuming date is like YYYY-MM-DD, or you can explode the time to create an timestamp
// assuming your date field in the database is date
// you can remove FROM_UNIXTIME if your field is not date

$startdate = strtotime($_GET['from_date']);
$enddate = strtotime($_GET['to_date']);


$addQuery = "";

if(isset($startdate)){
     $addQueryStart = "created >= FROM_UNIXTIME($startdate)";
}
if(isset($startdate)){
     $addQueryEnd = "created <= FROM_UNIXTIME($enddate)";
}

if(isset($startdate) && isset($enddate)){
    $addQuery .= "AND (".$addQueryStart." AND ".$addQueryEnd.")";
} elseif(isset($startdate) && !isset($enddate)){
    $addQuery .= "AND ".$addQueryStart;
} elseif(!isset($startdate) && isset($enddate)){
    $addQuery .= "AND ".$addQueryEnd;
}



$sql = "
    SELECT id, sso, full_name, case_status, assigned_to, resolution, description, updated, created, additional_notes, subject 
    FROM rmstable2 
    WHERE 
        sso LIKE '%$sso%' AND 
        case_status LIKE '%$case_status%' AND 
        assigned_to LIKE '%$assigned_to%' AND 
        subject LIKE '%$subject%' ".$addQuery;

后期编辑:针对不同情况

于 2012-08-08T14:44:27.670 回答