验证多行之间的数据

时间:2014-10-09 08:34:23

标签: php mysql

MySQL数据库:

date       | avl | occ
----------------------
2014-11-12 |  2  |  3
2014-11-13 |  0  |  5
2014-11-14 |  1  |  4

我正在使用以下查询从MySQL数据库中获取数据:

$results = $con->query("SELECT * FROM table_name WHERE date BETWEEN '" . $check_in_date . "' AND  '" . $check_out_date . "' ORDER by avl DESC");
if ($results) {
    while($row = $results->fetch_array()) {
        $avl= $row['avl'];
        $date= $row['date'];
    }
}

现在我只想在$check_in_date'$avl0之间$check_in_date'的值大于$check_out_date时打印{{1}}。

提前谢谢。

1 个答案:

答案 0 :(得分:3)

好吧,只需在WHERE子句中添加另一个条件。

示例:

$check_in_date = $con->real_escape_string($check_in_date);
$check_out_date = $con->real_escape_string($check_out_date);
$results = $con->query("SELECT * FROM table_name WHERE avl > 0 AND date BETWEEN '$check_in_date' AND '$check_out_date' ORDER by avl DESC");
if ($results->num_rows > 0) {
    while($row = $results->fetch_assoc()) {
        $avl = $row['avl'];
        $date = $row['date'];
    }
}