开发者

What is the difference between "column1 and(column2 or column3)" or "(column1 andcolumn2) or (column1 and column3) in an sql query run in mysql "

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 an开发者_如何学Pythond (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

A, B, C, then A & (B | C) == (A & B) | (A & C)

Well, I have tested both queries with simple example below both queries gives same result.

   create table emp_temp(id smallint(5),fname varchar(10),lname varchar(10));

    insert into emp_temp values (1,'jon','kam'),(2,'ish','dalviv'),(3,'ctn','gado'),
    (4,'jin','jain'),(5,'niraj','yadav');

    select * from emp_temp;

    mysql> select * from emp_temp;
    +------+-------+--------+
    | id   | fname | lname  |
    +------+-------+--------+
    |    1 | jon   | kam    |
    |    2 | ish   | dalviv |
    |    3 | ctn   | gado   |
    |    4 | jin   | jain   |
    |    5 | niraj | yadav  |
    +------+-------+--------+
    5 rows in set (0.00 sec)

    Now comparing your two queries.

    select * from mytable 
    where column1=x and (column2=y or column3=z)

    select * from mytable 
    where (column1=x and column2=y) or (column1=x and column3=z)

    Consider

    column1 is id
    column2 is fname
    column3 is lname

    mysql> select * from emp_temp
        -> where id=1 and (fname='jon' or lname='yadav');
    +------+-------+-------+
    | id   | fname | lname |
    +------+-------+-------+
    |    1 | jon   | kam   |
    +------+-------+-------+
    1 row in set (0.01 sec)


    mysql> select * from emp_temp 
        -> where (id=1 and fname='jon') or ( id=1 and lname='yadav');

    +------+-------+-------+
    | id   | fname | lname |
    +------+-------+-------+
    |    1 | jon   | kam   |
    +------+-------+-------+
    1 row in set (0.01 sec)

    Both queries produces the same result.


Are any of the columns NULL?

true AND unknown => false
true OR unknown => true

etc.

I did not see if all combinations of NULL would cause the two expressions to differ, but it seems a possible culprit.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜