Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9269155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:02:40+00:00 2026-06-18T15:02:40+00:00

I am trying to run a query with multiple WHERE clauses. If I do

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:02:41+00:00Added an answer on June 18, 2026 at 3:02 pm

    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 a HAVING COUNT(DISTINCT DetailName) = $n, so that any selected rows should have all of them, something like:

    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.

    This will give you something like:

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

    You can, however, shorten this query; for example if you remove the detailid, and detailname from the GROUP BY clause and use the GROUP_CONCAT to select them in one row concatenated with , like this:

    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.

    This will give you something like:

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

    Note that: the HAVING COUNT(DISTINCT d.DetailName) = 3 will 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 >=.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I am trying to run this query in SQL but it is not
Hey guys I am getting this exception when trying to run the query Column
I am trying to run the following query: SELECT COUNT(*) FROM ( SELECT *
I'm currently trying to run a LINQ query over a MS SQL database. This
I am trying to run multiple sites using single code base and code base
I'm trying to run a query from a .NET page but I seem to
what I am trying to do is run a query multiple times over multiple
Trying to run a query, keep getting ERROR 1054 : SELECT * from my_table
I am getting the following error when trying to run this query in SQL
I am trying to run multiple queries on some mysql dbs from a php

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.