I am trying to run a query with multiple WHERE clauses. If I do a multiple search, it returns a record from a single criteria. I need this query to return a result that has ALL of the criteria instead of just one.
You can see it here.
Additionally, I have provided the code:
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 );
}
?>
Tables:
tblRestaurants (RestID, RestName)
tblLocations (LocationID, CityID, AreaID, CuisineID)
tblLocDet (DetailID, LocationID)
tblDetails (DetailID, DetailName, DetailType)
To ensure that all the selected rows have all the items in the
$criteria, one way to do so is to count those items in this criteria variable and then having aHAVING COUNT(DISTINCT DetailName) = $n, so that any selected rows should have all of them, something like:SQL Fiddle Demo.
This will give you something like:
You can, however, shorten this query; for example if you remove the
detailid, anddetailnamefrom theGROUP BYclause and use theGROUP_CONCATto select them in one row concatenated with,like this:Updated SQL Fiddle Demo.
This will give you something like:
Note that: the
HAVING COUNT(DISTINCT d.DetailName) = 3will give you all the rows that have all the details names = 3 if you want to get those rows that had at least change it to>=.