I have a table in a mysql database that contains a lot of information. I'm only interested in those rows that contain specific info.

For example: Let's say each row contains the following two fields 1) Site and 2) FruitType. The Fruit type field could contain dozens of types of fruit, but I'm only interested in Apples and Oranges.

I have a search form with only one field that allows the user to select a specific site. They click submit, it POSTS the Site they searched for to the display.php page and then I want to have a SELECT statement that will select the specific site they searched for and preferribly only those rows in the database that contain Apples and Oranges.

$sql = "SELECT * FROM transactions WHERE site = '$site_query' AND fruittype = 'apples' OR fruittype= 'oranges' ORDER BY site, fruittype";

Obviously the problem with this query is the result is that I have selected rows that contain BOTH the Site I searched for AND apples, and I also end up with any rows that contain oranges even if the site doesn't match.

There is either an operator I'm missing that I could use in the SELECT statement or I am simply looking at this the wrong way. Any words of advice?

    Try using parenthesis

     $sql = "SELECT * FROM transactions WHERE site = '$site_query' AND (fruittype = 'apples' OR fruittype= 'oranges') ORDER BY site, fruittype"; 

      Excellent, that did the trick. I figured it would be something simple.

      Thanks again!

        you're welcome.

        When it comes to explaining I suck at it but I'll try.

        Without parenthesis the query searches for the values seperately.
        When you use parenthesis it searches for the values collectively.

        See what I mean? I know how it works but I can't explain it. Maybe someone else will.

          Write a Reply...