表A:
数据: 
a    b   c   d
001  bbb 1   ddd
002  qwe 2   dw
003  ece 1   wfew
004  ccs 3   verwve
005  ccs 3   verwve
表B:
数据: 
a    b   e   f
001  bbb 0   ddd
002  qwe 1   dw
003  ece 2   wfew
004  ccs 3   verwve
005  ccs 2   verwve
我想得到像下边一样的结果...a    b   c   d   e   f
     理科               --这些项是固定要有的。。(为了追加一条固定数据行,写了union all)
001  bbb 1   ddd 1   ddd--这些数据结果是随便写的
002  bbb 1   ddd 1   ddd
     文科               --这些项是固定要有的。。
003  bbb 1   ddd 1   ddd
     医学               --这些项是固定要有的。。
     科学               --这些项是固定要有的。。
004  bbb 1   ddd 1   ddd
为了实现我是这么写的但太复杂,请指教简单的方法(因为固定的数据要10个,所以我要写10次,所以代码很长)(select 
''
,'理科'
,''
,''
,''
,''
union all
select 
A.a
,A.b
,A.c
,A.d
,B.e
,B.f
from A
left outer join B on A.a=B.a 
                  and B.e='1'where A.c ='1'
)union all
(
select 
''
,'文科'
,''
,''
,''
,''
union all
select 
A.a
,A.b
,A.c
,A.d
,B.e
,B.f
from A
left outer join B on A.a=B.a 
                  and B.e='2'
where A.c ='2'
)union all
(
select 
''
,'医学'
,''
,''
,''
,''
union all
select 
A.a
,A.b
,A.c
,A.d
,B.e
,B.f
from A
left outer join B on A.a=B.a 
                  and B.e='5'where A.c ='3'
)
union all
(
select 
''
,'科学'
,''
,''
,''
,''
union all
select 
A.a
,A.b
,A.c
,A.d
,B.e
,B.f
from A
left outer join B on A.a=B.a 
                  and B.e='4'
where A.c ='4'
)

解决方案 »

  1.   

    这个用grouping()函数应该可以吧,不过效率不怎么高可能。
      

  2.   


    select A.*,B.e,B.f from A left join B on A.a=B.a and A.b=B.b--如果你固定10个。那应该写10次union all。
      

  3.   

    做个(临时)表,然后用union allselect * from
    (
    select '' a , '理科' b, '' c, '' d, '' e , 1 px union
    select '' a , '文科' b, '' c, '' d, '' e , 2 px union
    select '' a , '医学' b, '' c, '' d, '' e , 3 px union
    select '' a , '科学' b, '' c, '' d, '' e , 4 px union
    ...
    ) m
    union all
    (你的那些查询,及自己设计一个对应上面的排序字段px) n
    order by px
    select a , b, c, d, e from
    (
    select * from
    (
    select '' a , '理科' b, '' c, '' d, '' e , 1 px union
    select '' a , '文科' b, '' c, '' d, '' e , 2 px union
    select '' a , '医学' b, '' c, '' d, '' e , 3 px union
    select '' a , '科学' b, '' c, '' d, '' e , 4 px union
    ...
    ) m
    union all
    (你的那些查询,及自己设计一个对应上面的排序字段px) n
    ) t
    order by px
      

  4.   

    你如果能按照下面的说明提供详细的信息,我可以帮你写.
    最好给出完整的表结构,测试数据,计算方法和正确结果.否则耽搁的是你宝贵的时间。
    如果有多表,表之间如何关联?
    发帖注意事项
    http://topic.csdn.net/u/20091130/21/fb718680-98ff-4afb-98d8-cff2f8293ed5.html?24281
      

  5.   


    select *
    from(
    select 
    A.a
    ,A.b
    ,A.c
    ,A.d
    ,B.e
    ,B.f
    ,px = (case when B.e = '1' and A.c = '1' then 2
                when B.e = '2' and A.c = '2' then 4
                when B.e = '5' and A.c = '3' then 6)
    from A
    left outer join B on A.a=B.a
    union all
    select 
    ''
    ,'理科'
    ,''
    ,''
    ,''
    ,''
    ,px = 1
    union all
    select 
    ''
    ,'文科'
    ,''
    ,''
    ,''
    ,''
    ,px = 3
    union all
    ...
    )U
      

  6.   


    select *
    from(
    select 
    A.a
    ,A.b
    ,A.c
    ,A.d
    ,B.e
    ,B.f
    ,px = (case when B.e = '1' and A.c = '1' then 2
                when B.e = '2' and A.c = '2' then 4
                when B.e = '5' and A.c = '3' then 6
                ...)--继续添进去!
    from A
    left outer join B on A.a=B.a
    union all
    select 
    ''
    ,'理科'
    ,''
    ,''
    ,''
    ,''
    ,px = 1
    union all
    select 
    ''
    ,'文科'
    ,''
    ,''
    ,''
    ,''
    ,px = 3
    union all
    ...
    )U
    order by px
      

  7.   

    表里面的行一般没有顺序的,也就是不分先后。
    如果你要想到是后面xx些记录是哪一个科目的。就像上1楼说的一样,加一个学科字段就o了