使用php在多个mysql行之间验证true

时间:2014-10-20 05:34:20

标签: php mysql

MySQL数据库:

date       | avl
-----------------
2014-12-22 |  6
2014-12-23 |  3
2014-12-24 |  0
2014-12-25 |  2

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

$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'];
    }
}

此查询仅跳过avl列中值为0的行。但是如果在$ check_in_date和$ check_out_date之间的任何行中没有0的行中有0和true的话,我希望它返回false。

提前谢谢。

2 个答案:

答案 0 :(得分:0)

$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'];
    }
}

在avl>中使用括号() 0和条件之间

答案 1 :(得分:0)

尝试以下

  $check_in_date = $con->real_escape_string($check_in_date);
  $check_out_date = $con->real_escape_string($check_out_date);
  $results = $con->query(
  "SELECT date,
   CASE  WHEN avl > 0 THEN 'true'
   ELSE 'false' END as  myavl
 FROM table_name WHERE 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['myavl'];
    $date = $row['date'];
      }
    }
相关问题