I m stuck in a situation where I needed (column1 and column2) or (column1 or column3) from a table. So i implemented it as
select * from mytable
where column1=x and (column2=y or column3=z)
But it fetches me some unneccesory rows and by implementing as
select * from mytable
where (column1=x and column2=y) or (column1=x and column3=z)
It gives the result but i couldn’t understand the diff between the two…please suggest
EDIT (added details)
Below I have explained my situation, Please check this,
Let me elaborate my situation :::
I have a table, say clientdetails(int id, var firstname, var mobileno, var landlineno) and I need to fetch those entries fetching values having unique (firstname and mobileno), or (firstname and landlineno). Either of the two mobileno or landlineno is mandatory.
so i wrote a query…
select id
from clientdetails
where firstname = 'pooja'
and (mobileno = mn or landlineno= ln )
and mobileno REGEXP '^[0-9]+$'
and landlineno REGEXP '^[0-9]+$'"
Now ln or mn can be anything and say ”. Since there are many instances where the firstname is “pooja” without a landlineno. So it fetches that entries too which has no landlineno but different mobileno..
When I use the following query
select id
from clientdetails
where (firstname = 'pooja' and mobileno = mn)
or (firstname = 'pooja' and landlineno= '' )
and mobileno REGEXP '^[0-9]+$'
and landlineno REGEXP '^[0-9]+$'"
It fetches me the required rows.
Please explain me the execution format of these queries
So as Alnitak pointed out in comments, There shouldn’t be any difference – given three boolean variables
Well, I have tested both queries with simple example below both queries gives same result.