日期:2014-05-17  浏览次数:20957 次

#######################一个SQL问题where a='A' and b='B' or c='C' 和where a='A' and (b='B' or c='C')结果一样吗?
where   a= 'A '   and   b= 'B '   or   c= 'C '

where   a= 'A '   and   (b= 'B '   or   c= 'C ')
查询结果有什么不一样吗?为什么?
非常感谢!

------解决方案--------------------
当然不一样了
LOVE2008> select * from test
2 where id=6 and name= 'zhouxuan ' or addr=2;

ID NAME ADDR
---------- -------------------- ----------
7 zhouxuan 2
LOVE2008> select * from test
2 where id=6 and name= 'zhouxuan ' and addr=2;

no rows selected

LOVE2008>

为什么自己不试一下那
------解决方案--------------------
1。不一样
2。三个条件 a= 'A ' and b= 'B ' or c= 'C '
a= 'A ' and b= 'B '
||或者
c= 'C '
a= 'A ' and (b= 'B ' or c= 'C ')
a= 'A ' 并且 (b= 'B ' 或者 c= 'C ')
自己检查一下结果集不就知道了么
------解决方案--------------------
where a= 'A ' and b= 'B ' or c= 'C '
where a= 'A ' and (b= 'B ' or c= 'C ')
如果有c= 'C '的记录就不一样,反之结果一样
假设结果中有满足c= 'C '的记录,那么第一个查询出来的是所有c= 'C '的记录,不管a和b
而第二个查询出来的是c= 'C '与a= 'A '的交集,这就是不同
假设记录中没有满足c= 'C '的记录那么两个写法得到的结果一样



------解决方案--------------------
where a= 'A ' and b= 'B ' or b= 'C '与where a= 'A ' and (b= 'B ' or b= 'C ')的区别。
where a= 'A ' and b= 'B ' or b= 'C '的查询结果是不是所有b= 'C '的纪录以及满足a= 'A ' and b= 'B '条件的纪录的合集?  正确
where a= 'A ' and (b= 'B ' or b= 'C ')的查询结果是不是所有a= 'A '并且b是 'B '或 'C '的纪录?正确
------解决方案--------------------
完全不一样
where a= 'A ' and b= 'B ' or c= 'C '
其实是(a= 'A ' and b= 'B ') or c= 'C ' (a必须等于 'A ',b必须等于 'B ' 这两个条件必须同时满足)
怎么可能和 a= 'A ' and (b= 'B ' or c= 'C ') 一样。
a= 'A ' and (b= 'B ' or c= 'C ')(a必须等于 'A ',b= 'B ' 或者 c= 'C '任意成立都可以)