0

So I'm making a small database for a restaurant and I am trying to set up an interface for them to be able to select certain criteria and see how much of it they have in stock. For example, if they select "Meat" all of the different types of meat will be listed. This works fine when I query for only one type of meat like this:

SELECT a.name, b.type, a.cost, a.end, a.url 
FROM items a, type b 
WHERE b.type = a.type 
AND b.type="beef";

The result I'd get for that query would list the correct # of elements and so on.

sample result: enter image description here

However if I add an "OR" clause to try to call every type of meat in stock, for example:

SELECT a.name, b.type, a.cost, a.end, a.url 
FROM items a, type b 
WHERE b.type = a.type 
AND b.type="beef"
OR b.type="poultry";

the first few results will be correct but then it will add in random elements that don't fit any of the criteria and even change certain elements so that they don't match up, for example it will label bananas as poultry or something! I have no idea why it's doing this. It will also repeat a whole ton of items.

Sample results from addition of "or" clause: http://puu.sh/dj5aq/ea7876fb33.png

any idea what I'm doing wrong?

Let me know if you need any more info, I'm not sure if this is sufficient or not.

Itay
  • 16,601
  • 2
  • 51
  • 72
movac
  • 1,576
  • 3
  • 21
  • 45
  • 5
    use parenthesis `AND ( b.type="beef" OR b.type="poultry" )` – CRABOLO Dec 06 '14 at 08:18
  • 2
    What @cVplZ says. Consider the difference between that and `WHERE (b.type = a.type AND b.type="beef") OR b.type="poultry";` which is what you get with no parentheses and left to right evaluation. – RobP Dec 06 '14 at 08:19
  • `type IN ('beef','poultry')` – Strawberry Dec 06 '14 at 11:12
  • possible duplicate of [How exactly does using OR in a MySQL statement differ with/without parentheses?](http://stackoverflow.com/questions/4872180/how-exactly-does-using-or-in-a-mysql-statement-differ-with-without-parentheses) – CRABOLO Dec 07 '14 at 05:40

0 Answers0