mysql查询中的多个where语句

时间:2015-10-04 07:47:26

标签: php sql

我的php中有一个sql查询,看起来像这样

$query1 = "SELECT time, air FROM pi_sensors_network WHERE date BETWEEN '".$date1."' AND '".$date2."'";

但现在我想在我的sql查询中包含一个额外的where语句

WHERE location = '".$location."'";

当然我也有表格

<form action="" method="get">
        Select Your location:
        <select name="location" onchange="javascript: submit()">
        <option>---</option>
        <option>FC3</option>
        <option>T12</option>
        </select>
    </form>

以及从我的数据库中获取位置的php

<?php
    if(isset($_POST['location']))
    {
    echo "<b>".$_POST['location']."</b><br><br>";
    }
?>


<?php
$location = 'FC3';

    if( isset($_GET['location'] ))
    {
        $location = $_GET["location"];
    }
?>

如何在我的sql查询中包含多个where语句?

4 个答案:

答案 0 :(得分:1)

您可以使用AND在查询中执行多个WHERE条件。试试这样:

$query1 = "SELECT time, air 
FROM pi_sensors_network 
WHERE date BETWEEN '".$date1."' AND '".$date2."'
AND location = '".$location."'";

答案 1 :(得分:0)

您可以根据需要使用WHEREAND分隔。{/ p>

mysql查询中的多个where语句

放置多个WHERE子句(如果你想到的话)。

$query1 = "SELECT time, air FROM pi_sensors_network 
WHERE date BETWEEN '".$date1."' AND '".$date2."'" 
AND location = '".$location."'";

或者这个:

$query1 = "SELECT time, air FROM pi_sensors_network 
WHERE location = '".$location."'" AND date BETWEEN '".$date1."' 
AND '".$date2."'";
  

注意:两者都有效,只取决于你的情景/选择   有

答案 2 :(得分:0)

如果您需要更多“where-statements”在第一个WHERE语句之后,您将它们写为:AND

在你的例子中:

finally

答案 3 :(得分:0)

如果设置了位置,则新条件将插入查询中,如果不是,则为空。

if( isset($_GET['location'] ))
            {
                $location = $_GET["location"];
                $location_q = " AND location LIKE ".$location;
            } else 
            {
            $location_q = ""
            }
    $query1 = "SELECT time, air FROM pi_sensors_network WHERE date BETWEEN '".$date1."' AND '".$date2.$location_q"'";