现在有一张oracle表是这样:
id   name   age  sex
a    张三    10    男
a    李四    15    女
b    王五    20    男
b    赵六    50    男
c    王七    45    男怎样用sql实现这样的输出??
id   name   age  sex  id   name   age  sex   id   name    age   sex
a    张三    10     男    b    王五    20     男    a    李四      15     女
a    李四    15     女    b    赵六    50     男    b    王五      20     男
                                                                     b    赵六     50     男
                                                                     c    王七     45     男
就是对同一张表根据不同条件(id=a、id=b、age>10),输出成上面那样
跪求各位老师大神!!!

解决方案 »

  1.   


    就是对一张表,把不同条件的select结果放在一起输出,比如前四列是根据id=a条件select出的结果,中间四列是根据id=b条件select出的,最后四列是根据age>10条件select出的,我就是想问怎么能这三个条件的结果合在一起
      

  2.   

    就是对一张表,把不同条件的select结果放在一起输出,比如前四列是根据id=a条件select出的结果,中间四列是根据id=b条件select出的,最后四列是根据age>10条件select出的,我就是想问怎么能这三个条件的结果合在一起
      

  3.   

    就是对一张表,把不同条件的select结果放在一起输出,比如前四列是根据id=a条件select出的结果,中间四列是根据id=b条件select出的,最后四列是根据age>10条件select出的,我就是想问怎么能这三个条件的结果合在一起
      

  4.   

    select 
    a.id,a.name,a.age,a.sex,b.id,b.name,b.age,b.sex,c.id,c.name,c.age,c.sex
    from (select id,name,age,sex,rownum rn from table where id='a') a
    full join (select id,name,age,sex,rownum rn from table where id='b') b
    on a.rn=b.rn
    full join (select id,name,age,sex,rownum rn from table where age>10) c
    on a.rn=c.rn;
    这样子?
      

  5.   

    如果 a.rn=b.rn 报错的话就再嵌套子查询
      

  6.   


    但是如果没有满足id=a条件的结果,那最后的数据就变成这样了
    id   name   age  sex  id   name   age  sex  id   name   age  sex
                                       b    王五   20    男  
                                       b    赵六   50    男 
                                                                         a    李四   15    女
                                                                         b    王五   20    男
                                                                         b    赵六   50    男
                                                                         c    王七   45    男后面四列的数据就会空出好多行,有办法能变成这样吗??id   name   age  sex  id   name   age  sex  id   name   age  sex
                                       b    王五   20    男     a    李四   15    女
                                       b    赵六   50    男     b    王五   20    男
                                                                        b    赵六   50    男
                                                                        c    王七   45    男
                                                                         
                                                                        
      

  7.   


    但是如果没有满足id=a条件的结果,那最后的数据就变成这样了
    id   name   age  sex  id   name   age  sex  id   name   age  sex
                                       b    王五   20    男  
                                       b    赵六   50    男 
                                                                         a    李四   15    女
                                                                         b    王五   20    男
                                                                         b    赵六   50    男
                                                                         c    王七   45    男后面四列的数据就会空出好多行,有办法能变成这样吗??id   name   age  sex  id   name   age  sex  id   name   age  sex
                                       b    王五   20    男     a    李四   15    女
                                       b    赵六   50    男     b    王五   20    男
                                                                        b    赵六   50    男
                                                                        c    王七   45    男
                                                                         
                                                                        
    考虑过这个问题,不过看你的结果不用就懒了,要实现上述这样,无论哪个a或者b没有的话,把a和b全连接做子查询再全连接c。
    with table1 as(
    select 'a' id ,'张三' name,'10' age,'男' sex from dual union all
    select 'a' id ,'李四' name,'15' age,'女' sex from dual union all
    select 'b' id ,'王五' name,'20' age,'男' sex from dual union all
    select 'b' id ,'赵六' name,'50' age,'男' sex from dual union all
    select 'c' id ,'王七' name,'45' age,'男' sex from dual)
    select t.id,t.name,t.age,t.sex,t.id1,t.name1,t.age1,t.sex1,c.id,c.name,c.age,c.sex
    from (select 
    a.id,a.name,a.age,a.sex,b.id id1,b.name name1,b.age age1,b.sex sex1,nvl(a.rn,b.rn) rn
    from (select id,name,age,sex,rownum rn from table1 where id='a') a
    full join (select id,name,age,sex,rownum rn from table1 where id='b') b
    on a.rn=b.rn) t
    full join (select id,name,age,sex,rownum rn from table1 where age>10) c
    on t.rn=c.rn;
      

  8.   


    那要说是在这个基础上 想让最后的结果但是如果没有满足id=a条件的结果,那最后的数据就变成这样了
    id   name   age  sex  id   name   age  sex  id   name   age  sex
                                       b    王五   20    男  
                                       b    赵六   50    男 
                                                                         a    李四   15    女
                                                                         b    王五   20    男
                                                                         b    赵六   50    男
                                                                         c    王七   45    男后面四列的数据就会空出好多行,有办法能变成这样吗??id   name   age  sex  id   name   age  sex  id   name   age  sex
                                       b    王五   20    男     a    李四   15    女
                                       b    赵六   50    男     b    王五   20    男
                                                                        b    赵六   50    男
                                                                        c    王七   45    男
                                                                         
                                                                        
    考虑过这个问题,不过看你的结果不用就懒了,要实现上述这样,无论哪个a或者b没有的话,把a和b全连接做子查询再全连接c。
    with table1 as(
    select 'a' id ,'张三' name,'10' age,'男' sex from dual union all
    select 'a' id ,'李四' name,'15' age,'女' sex from dual union all
    select 'b' id ,'王五' name,'20' age,'男' sex from dual union all
    select 'b' id ,'赵六' name,'50' age,'男' sex from dual union all
    select 'c' id ,'王七' name,'45' age,'男' sex from dual)
    select t.id,t.name,t.age,t.sex,t.id1,t.name1,t.age1,t.sex1,c.id,c.name,c.age,c.sex
    from (select 
    a.id,a.name,a.age,a.sex,b.id id1,b.name name1,b.age age1,b.sex sex1,nvl(a.rn,b.rn) rn
    from (select id,name,age,sex,rownum rn from table1 where id='a') a
    full join (select id,name,age,sex,rownum rn from table1 where id='b') b
    on a.rn=b.rn) t
    full join (select id,name,age,sex,rownum rn from table1 where age>10) c
    on t.rn=c.rn;那要说是在这个基础上 想让最后的每个小结果集按 age 倒叙输出呢
    id   name   age  sex  id   name   age  sex   id   name    age   sex
                                    b    赵六     50    男       e    赵六     50    男
                                    b    王五     20    男       c    王七     45    男
                                                                         f     王五     20    男
                                                                         a    李四     15    女
      

  9.   

    你真的有理解这个sql吗? 倒叙只要在子查询里面加排序就好了啊
    像这样 select id,name,age,sex,rownum rn from table1 where id='a' order by age desc
      

  10.   


    oracle子查询不支持排序吧,加了这句话可以排序,但是full join后就不行了
      

  11.   


    with table1 as(
    select 'a' id ,'张三' name,'10' age,'男' sex from dual union all
    select 'a' id ,'李四' name,'15' age,'女' sex from dual union all
    select 'b' id ,'王五' name,'20' age,'男' sex from dual union all
    select 'b' id ,'赵六' name,'50' age,'男' sex from dual union all
    select 'c' id ,'王七' name,'45' age,'男' sex from dual)
    select t.id,t.name,t.age,t.sex,t.id1,t.name1,t.age1,t.sex1,c.id,c.name,c.age,c.sex
    from (select 
    a.id,a.name,a.age,a.sex,b.id id1,b.name name1,b.age age1,b.sex sex1,nvl(a.rn,b.rn) rn
    from (select id,name,age,sex,row_number() over(order by age desc) rn from table1 where id='a') a
    full join (select id,name,age,sex,row_number() over(order by age desc) rn from table1 where id='b') b
    on a.rn=b.rn) t
    full join (select id,name,age,sex,row_number() over(order by age desc) rn from table1 where age>10) c
    on t.rn=c.rn;