我正在尝试使用多个 WHERE 子句运行查询。如果我进行多项搜索,它会从单个条件返回一条记录。我需要这个查询来返回一个包含所有条件而不仅仅是一个条件的结果。
你可以在这里看到它。
另外,我提供了代码:
if (isset ( $_POST ["btnSearch"] )) {
echo "<br>Selected Options are :<br>";
$checked = $_POST ["criteria"];
$criteria = "";
$separator = ", ";
for($i = 0; $i < count ( $checked ); $i ++) {
echo " " . $checked [$i] . "<br/>";
if ($i == count ( $checked ) - 1) {
$separator = "";
}
$criteria = $criteria . "'" . $checked [$i] . "'" . $separator;
}
echo "<br><br>";
echo $criteria . "<br><br>";
include "config.php";
mysql_select_db ( "MyHead", $con );
//$DM = implode(',',$criteria);
$mysqlQuery = "SELECT tblRestaurants.RestName, tblLocDet.LocationID, tblLocDet.DetailID, tblDetails.DetailName, tblRestaurants.RestName
FROM tblRestaurants INNER JOIN (tblLocations INNER JOIN (tblLocDet INNER JOIN tblDetails ON tblLocDet.DetailID = tblDetails.DetailID) ON tblLocations.LocationID = tblLocDet.LocationID) ON tblRestaurants.RestID = tblLocations.RestID
GROUP BY tblRestaurants.RestName, tblLocDet.LocationID, tblLocDet.DetailID, tblDetails.DetailName
HAVING tblDetails.DetailName IN (" . $criteria . ");";
if (! $rs = mysql_query ( $mysqlQuery )) {
echo "Cannot parse query";
} elseif (mysql_num_rows ( $rs ) == 0) {
echo "No records found";
} else {
echo "<table id=\"myTable\" table width=\"710\" class=\"beautifuldata\" align=\"Left\" cellspacing=\"0\">\n";
echo "<thead>\n<tr>";
echo "<th>PLACE</th>";
echo "<th>ADDRESS</th>";
echo "<th>PHONE</th>";
echo "<th>PRICE</th>";
echo "<th>RATING</th>";
echo "</tr>\n</thead>\n";
while ( $row = mysql_fetch_array ( $rs ) ) {
echo "<tr><td><strong><a href='" . $row [RestPage] . "'>" . $row ['RestName'] . "</a></strong></td>";
echo "<td>" . $row ['DetailName'] . "</td>";
echo "<td>" . $row ['Phone'] . "</td>";
echo "<td>" . $row ['Price'] . "</td>";
echo "<td>" . $row ['Rating'] . "</td>";
echo "</tr>";
}
}
echo "</table><br />\n";
mysql_close ( $con );
}
?>
表:
tblRestaurants (RestID, RestName)
tblLocations (LocationID, CityID, AreaID, CuisineID)
tblLocDet (DetailID, LocationID)
tblDetails (DetailID, DetailName, DetailType)