MySQL多个“AND”语句

时间:2013-02-10 01:51:25

标签: php mysql

我正在尝试使用多个WHERE子句运行查询。如果我进行多重搜索,它会从单个条件返回记录。我需要此查询来返回具有所有条件的结果,而不仅仅是一个。

你可以看到它here

此外,我提供了以下代码:       

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)

3 个答案:

答案 0 :(得分:2)

要确保所有选定的行都包含$criteria中的所有项目,一种方法是在此条件变量中计算这些项目,然后使用HAVING COUNT(DISTINCT DetailName) = $n,以便任何选定的行应该包含所有这些,例如:

SELECT 
  r.RestName, 
  ld.LocationID, 
  ld.DetailID, 
  d.DetailName
FROM tblRestaurants     AS r
INNER JOIN tblLocations AS l  ON r.RestID     = l.RestID
INNER JOIN tblLocDet    AS ld ON l.LocationID = ld.LocationID
INNER JOIN
(
  SELECT l.Locationid
  FROM tblLocDet l
  INNER JOIN tbldetails d ON l.detailid = d.detailid
  WHERE d.detailname IN ('det1', 'det2', 'det3')
  GROUP BY l.locationid
  HAVING COUNT(DISTINCT DetailName) = $n
)                       AS ld2 ON ld.locationid = ld2.locationid
INNER JOIN tblDetails   AS d   ON ld.DetailID   = d.DetailID   
GROUP BY r.RestName, 
         ld.LocationID, 
         ld.DetailID, 
         d.DetailName;

SQL Fiddle Demo

这会给你类似的东西:

| RESTNAME | LOCATIONID | DETAILID | DETAILNAME |
-------------------------------------------------
|     res1 |          1 |        1 |       det1 |
|     res1 |          1 |        2 |       det2 |
|     res1 |          1 |        3 |       det3 |

但是,您可以缩短此查询;例如,如果您从detailid子句中删除detailnameGROUP BY,并使用GROUP_CONCAT在与,连接的一行中选择它们,如下所示:

SELECT 
  r.RestName, 
  ld.LocationID, 
  GROUP_CONCAT(DISTINCT d.DetailName separator ',') Details
FROM tblRestaurants     AS r
INNER JOIN tblLocations AS l  ON r.RestID     = l.RestID
INNER JOIN tblLocDet    AS ld ON l.LocationID = ld.LocationId
INNER JOIN tblDetails   AS d  ON ld.DetailID  = d.DetailID   
WHERE d.detailname IN ('det1', 'det2', 'det3')
GROUP BY r.RestName, 
         ld.LocationID
HAVING COUNT(DISTINCT d.DetailName) = 3;

Updated SQL Fiddle Demo

这会给你类似的东西:

| RESTNAME | LOCATIONID |        DETAILS |
------------------------------------------
|     res1 |          1 | det3,det2,det1 |

请注意: HAVING COUNT(DISTINCT d.DetailName) = 3将为您提供具有所有详细信息名称= 3的所有行,如果您想获取至少将其更改为>=

答案 1 :(得分:0)

您需要此查询:

SELECT tblRestaurants.RestName, ...
FROM tblRestaurants 
INNER JOIN tblLocations ON tblLocations.RestID = tblRestaurants.RESTID
INNER JOIN tblLocDet ON tblLocDet.LocationID = tblLocations.LocationID
INNER JOIN tblDetails ON tblLocDet.DetailID  = tblDetails.DetailID
WHERE tblDetails.DetailName IN (" . $criteria . ");";
ORDER BY...

由于没有聚合函数(MIN,MAX等),因此不需要GROUP BY。使用ORDER BY对列表进行排序。

移植到mysqli_ *(或PDO)函数并使用预处理语句来避免SQL注入也是一个好主意

答案 2 :(得分:0)

这是一个解决方案:

<?php
if (isset ( $_POST ["btnSearch"] )) {
$checked = (array) $_POST ["criteria"];
sort($checked);
$criteria = implode(",", $checked);
echo "<br>Selected Options are :<br>" . $criteria . "<br><br>";

include "config.php";

mysql_select_db ( "MyHead", $con );

$criteria = mysql_real_escape_string($criteria);

$mysqlQuery = "
SELECT r.RestName, ld.LocationID, ld.DetailID, d.DetailName, r.RestName
FROM tblRestaurants AS r
INNER JOIN tblLocations AS l 
  ON r.RestID = l.RestID
INNER JOIN tblLocDet AS ld 
  ON l.LocationID = ld.LocationID 
INNER JOIN tblDetails AS d 
  ON ld.DetailID = d.DetailID
INNER JOIN (
  SELECT DetailID, GROUP_CONCAT(DetailName ORDER BY DetailName) AS DetailList 
  FROM tblDetails GROUP BY DetailID) AS dx
  ON ld.DetailID = dx.DetailID
WHERE dx.DetailList = '$criteria'";

if (! $rs = mysql_query ( $mysqlQuery )) {
  . . . 

其余部分与您的代码相同。