当我向其添加日期范围时,我的查询将无效。使用php和MySQL

时间:2012-08-08 14:35:01

标签: php mysql

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

这是有效的查询:(由于某些原因,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 个答案:

答案 0 :(得分: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

如何将AND更改为OR

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

答案 1 :(得分: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

答案 2 :(得分:0)

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

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

甚至使用BETWEEN如:

AND DATE(created) BETWEEN '$startdate' AND '$enddate'

答案 3 :(得分: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;

稍后编辑:针对不同情况