union all 的执行顺序

IQ下面做应用开发的时候,遇到一个好玩的事情:

情况1中,发现一个规律,union all 中执行顺序是:
以第一个union all 的select 语句(如 红色标记)为分界线。
先从最后union all的select(如蓝色标记)开始倒序输出到 红色标记的select子句,
再输出第一个select子句
最后输出红色标记的select子句

        select '1' AREA_NAME
        union all 
	[color=red]select '3' AREA_NAME [/color]
        union all 
        select distinct  '2'AREA_NAME
        from DW.AREA
        where (area_type_id =2 and  up_area_code = '00' ) or  up_area_code = '-1' 
        union all 
        [color=blue]select '4' AREA_NAME[/color]

结果如下:
AREA_NAME
4
2
1
3


        select '1' AREA_NAME
        union all 
        select distinct  '2'AREA_NAME
        from DW.AREA
        where (area_type_id =2 and  up_area_code = '00' ) or  up_area_code = '-1'        
        union all 
		select '3' AREA_NAME 
        union all 
        select '4' AREA_NAME

结果如下:
AREA_NAME
4
3
1
2


        select '1' AREA_NAME
        union all 
        select distinct  '2'AREA_NAME
        from DW.AREA
        where (area_type_id =2 and  up_area_code = '00' ) or  up_area_code = '-1'        
        union all 
		select '3' AREA_NAME 
        union all 
        select '4' AREA_NAME
         union all 
        select distinct  '5' AREA_NAME
        from DW.AREA
        where (area_type_id =2 and  up_area_code = '00' ) or  up_area_code = '-1'       


AREA_NAME
5
4
3
1
2


这里面表DW.AREA为真实表。

情况2,如果下面这种情况下:
       select '1' AREA_NAME
        union all 
	select '2' AREA_NAME
        union all 
        select '3' AREA_NAME
        union all 
        select '4' AREA_NAME

AREA_NAME
'1'
'4'
'3'
'2'

        select '2' AREA_NAME
        union all 
        select '1' AREA_NAME
        union all 
        select '4' AREA_NAME
        union all 
        select '3' AREA_NAME

AREA_NAME
'2'
'3'
'4'
'1'

如union all的表不存在的时候,即单纯select子句,输出顺序就变成了:
1.第一select 输出。
2.此后从最后一个select倒序输出。

上述为我目前看到的现象。
具体什么原因,等后面有时间了的话,会好好探究的。
估计和cursor生成的信息有关。mark 一下。

猜你喜欢

转载自yangyoupeng-cn-fujitsu-com.iteye.com/blog/1723191