0

我有 3 种不同类型的查询来检索同一页面中的数据。我将如何完全结合?以下是查询:

<?php
$sql = "SELECT * FROM general WHERE ((day1sql >= now()))";
if($_GET!=""){
$mydate = mysql_real_escape_string($_GET['datepicker']);
if($mydate!=""){    
$sql = "SELECT * FROM general WHERE ((day1 = '$mydate'))";  
}       
}
if($_GET!=""){
$city1 = mysql_real_escape_string($_GET['cityText']);
if($city1!=""){ 
$sql = "SELECT * FROM general WHERE (city = $city1))";  
}       
}

datepicker 是一个日历,用户可以根据日期获取数据。并且有一个下拉菜单,用户可以选择城市名称。前两个查询工作正常。我很困惑如何整合第三个查询?

4

2 回答 2

2

您可以使用逻辑OR运算符来组合您的过滤条件:

SELECT * FROM general WHERE day1sql >= now() OR day1 = '$mydate' OR city = '$city1'
于 2012-09-02T11:40:42.740 回答
0

像这样的东西可能会起作用:

sql = "SELECT * FROM general WHERE (day1sql >= now())";

if(isset($_GET['datepicker'])){
    $mydate = mysql_real_escape_string($_GET['datepicker']);
    $sql .= " AND (day1 = '$mydate')";         
}
if(isset($_GET['cityText'])){
    $city1 = mysql_real_escape_string($_GET['cityText']);
    $sql .= " AND (city = $city1)";  
}       

// execute $sql

我们在 sql 后面加上 AND 条件。我相信这就是你想要做的。

于 2012-09-02T11:48:33.753 回答