oracle where and or语句 同时包含 or 和and这种怎么弄

1882人阅读
Hive(41)
mysql(31)
oracle(106)
SQL的WHERE子句中包含多个AND和OR
SQL的WHERE子句中包含多个AND和OR
关于 sql的where子句中包含多个and和or是计算顺序问题,SQL在处理操作时会优先处理and操作,这就是很多人会得到很多错误的原因,举个例子:
加入有表product字段如下:id、product_id、product_price、product_name
现在要查找产品号为100或者101,并且价格大于200的商品,你可能会这样做:
select&*&from&product&where&product_id&=&100&or&product_id&=&101&and&product_price&&&200
这是错误的,SQL会这样理解,SQL首先进行and 的操作,最后 就成了这个意思:查找 产品号为101并且价格大于200 或者 产品号为100 的商品。
解决办法很简答,只要使用圆括号就行了:
select&*&from&product&where&(product_id&=&100&or&product_id&=&101)&and&product_price&&&200
===============今天又被这个问题坑了===============
SELECT&person_id,&first_name,&last_name&FROM&people&WHERE&first_name&like&:first_name&or&last_name&like&:last_name&AND&((person_id&&&:_person_id))&ORDER&BY&person_id&ASC&LIMIT&2
我是执行这条sql 出现的这个问题。
SELECT&*&FROM&people&;
person_id&&first_name&&last_name&&
1&&&&&&&&&&JOHN&&&&&&&&DOE&&&&&&&&
2&&&&&&&&&&JOHN&&&&&&&&DOE&&&&&&&&
3&&&&&&&&&&JOHN&&&&&&&&DOE&&&&&&&&
4&&&&&&&&&&JOHN&&&&&&&&DOE&&&&&&&&
5&&&&&&&&&&JOHN&&&&&&&&DOE&&&&&&&&
6&&&&&&&&&&JOHN&&&&&&&&DOE&&&&&&&&
7&&&&&&&&&&JOHN&&&&&&&&DOE&&&&&&&&
8&&&&&&&&&&JOHN&&&&&&&&DOE
SELECT&person_id,&first_name,&last_name&FROM&people&WHERE&first_name&like&'%JOHN%'&or&last_name&like&'%DOE%'&AND&((person_id&&&2))&ORDER&BY&person_id&ASC&LIMIT&2
person_id&&first_name&&last_name&&
1&&&&&&&&&&JOHN&&&&&&&&DOE&&&&&&&&
2&&&&&&&&&&JOHN&&&&&&&&DOE
or的前一个条件总是成立,总是返回前两个记录,不管person_id 的大小。
AND 和 OR 运算符
AND 和 OR 可在 WHERE 子语句中把两个或多个条件结合起来。
如果第一个条件和第二个条件都成立,则 AND 运算符显示一条记录。
如果第一个条件和第二个条件中只要有一个成立,则 OR 运算符显示一条记录。
or 运算符 只要有一个为 true ,or 表达式为true ,则显示该记录。
=================END=================
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:129322次
积分:2257
积分:2257
排名:第12817名
转载:618篇温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
在LOFTER的更多文章
loftPermalink:'',
id:'fks_',
blogTitle:'sql语句中and和or混合使用时需要注意事项',
blogAbstract:'AND、OR运算符的组合使用在WHERE子句中,通过AND、OR运算符可以同时连接多个条件,当然AND、OR运算符也可以同时使用。但是当AND、OR运算符同时存在时,其优先级如何确定呢?与大多数语言一样,SQL语言认为AND运算符的运算级别要高于OR运算符。即当出现',
blogTag:'',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:0,
publishTime:0,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'',
hmcon:'0',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}

我要回帖

更多关于 sql语句where or 的文章

 

随机推荐