使用Oracle的minus时注意order by

昨天发现一个很奇怪的现象,我在分页时的SQL明明是6条,结果确是意想不到的(是9条),我的SQL是这样的
select rownum,Page.* from(select id_item,itemname,id_itemclass,id_org from stm_list where id_item not in (select id_item from stm_exppsinfo where id_ptitem='P00002' and grpcode='200901' and IS_FENPEI='Y') and grpcode='200901' and id_ptitem='P00002' order by id_itemclass,id_org)Page where rownum<=66 MINUS select rownum,Page.* from(select id_item,itemname,id_itemclass,id_org from stm_list where id_item not in (select id_item from stm_exppsinfo where id_ptitem='P00002' and grpcode='200901' and IS_FENPEI='Y') and grpcode='200901' and id_ptitem='P00002' order by id_itemclass,id_org)Page where rownum<=60


查查了原因,原来是在使用order by的时候出问题了,本人的经验,在使用minus时同时又使用order by进行排序,这时候请加上第二个排序条件,而且这个排序的条件尽量不要重复,以免出现意想不到的结果,最好是主键。

总结:

1.oracle的minus是差集的意思
2.oracle的INTERSECT 是返回两个查询中都出现的记录
3.oracle的UNION 是返回两个查询的记录
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值