PHP mysql subquery filtering

I have a table like this:

id     price    condition     username
----------------------------------------
 1       20     New           Kim 
 2       30     Used          Kim
 3       40     Used          George
 4       60     New           Tom

My question is about the subquery. when i use this query

SELECT *
FROM `table`
WHERE `username` = 'kim'
  AND `price` = '20'
   OR `condition` = 'New' 

this becomes true for the last condition and brings 2 entries, where condition = 'New'

However, when I use AND, it returns the correct entry.

SELECT *
FROM `table`
WHERE `username` = 'kim'
  AND `price` = '20'
  AND `condition` = 'New'

I want to enclose the request so that I can select the price and condition only from the results of the first request, which is the username. Thanks

+3
source share
2 answers
SELECT * FROM table WHERE username = 'kim' AND (price = '20' OR condition = 'New');
+3
source

As Rafal Wojcik said, you need to use some brackets to tell the database what you are grouping.

Without any certainty, the engine groups all of your conventions as they appear:

username = 'kim' AND price = 20 OR condition = 'New' 

read like

((username = 'kim' AND price = 20) OR condition = 'New')

, , . OR "kim-20" "New"

AND.

!

+2

All Articles