表           a00_Module  :
a00code        a00name
1001       系统管理
1002       基础信息
2001       订单管理
2002       采购管理
2003       配货管理
2004       报表管理表a01_funinfo:
a00code       a01code   a01name
1001       1     初始设置
1001       2     权限管理
1002       3     商品类别
1002       4     扩展规格
1002       5     商品品牌
1002       6     商品信息
1002       7     商户信息
1002       8     客户信息
1002       9     数据词典想把这两张表连接在一起,相等的条件是a00code
并且连接后想用两张表统一的名称和编号1001       系统管理
1          初始设置
2               权限管理
1002       基础信息
2          权限管理
3          商品类别
4          扩展规格
5          商品品牌
6          商品信息
7          商户信息
8          客户信息
9          数据词典
并且按着两张表的a00Sort,a01Sort排序!

解决方案 »

  1.   

    select code = case a01code when 0 then a00code else a01code end
      ,a00name
    from (
      select a00code, a01code=0, a00name from a00_Module
      union 
      select a00code, a01code, a00name from a00_Module
    ) as a
    order by a00code, a01code
      

  2.   

    select a00code,a00name,a01code,a01name
    from a00_module a0,a01_funinfo a1
    where a0.a00code=a1.a00code
    order by a00code,a01code
      

  3.   


    tim_spac 没有加条件
    tim_spac 不是横向连接,是纵向连接!!
    继续期待中!
    不过,还是谢谢大家的热情!!
      

  4.   

    create table #a00_Module(a00code int,a00name varchar(10))
    insert #a00_Module select 1001,       '系统管理' 
    insert #a00_Module select 1002 ,      '基础信息' 
    insert #a00_Module select 2001  ,     '订单管理' 
    insert #a00_Module select 2002   ,    '采购管理' 
    insert #a00_Module select 2003    ,   '配货管理' 
    insert #a00_Module select 2004     ,  '报表管理' create table #a01_funinfo(a00code int,a01code int,a01name  varchar(10))
    insert #a01_funinfo select 1001,        1        ,'初始设置' 
    insert #a01_funinfo select 1001 ,       2       , '权限管理' 
    insert #a01_funinfo select 1002  ,      3      ,  '商品类别' 
    insert #a01_funinfo select 1002   ,     4     ,   '扩展规格' 
    insert #a01_funinfo select 1002    ,    5    ,    '商品品牌' 
    insert #a01_funinfo select 1002     ,   6   ,     '商品信息' 
    insert #a01_funinfo select 1002      ,  7  ,      '商户信息' 
    insert #a01_funinfo select 1002       , 8 ,       '客户信息' 
    insert #a01_funinfo select 1002        ,9,        '数据词典'
    go
    select a01code,a01name from(
    select 1 px, a00code,a01code,a01name from #a01_funinfo
    union
    select 0 px,a00code,a00code,a00name from #a00_module)a 
    order by a00code,px,a01code
    /*
    a01code     a01name    
    ----------- ---------- 
    1001        系统管理
    1           初始设置
    2           权限管理
    1002        基础信息
    3           商品类别
    4           扩展规格
    5           商品品牌
    6           商品信息
    7           商户信息
    8           客户信息
    9           数据词典
    2001        订单管理
    2002        采购管理
    2003        配货管理
    2004        报表管理
    */
    go
    drop table #a00_Module,#a01_funinfo 
      

  5.   

    楼上的加个条件就行
    union 
    select 0 px,a00code,a00code,a00name from a00_module b 
    where exists( select 1 from a01_funinfo where a00code = b.a00code) 
      

  6.   

    set nocount on
    go
    create table #a00_Module(a00code int,a00name varchar(10))
    insert #a00_Module select 1001,       '系统管理' 
    insert #a00_Module select 1002 ,      '基础信息' 
    insert #a00_Module select 2001  ,     '订单管理' 
    insert #a00_Module select 2002   ,    '采购管理' 
    insert #a00_Module select 2003    ,   '配货管理' 
    insert #a00_Module select 2004     ,  '报表管理' create table #a01_funinfo(a00code int,a01code int,a01name  varchar(10))
    insert #a01_funinfo select 1001,        1        ,'初始设置' 
    insert #a01_funinfo select 1001 ,       2       , '权限管理' 
    insert #a01_funinfo select 1002  ,      3      ,  '商品类别' 
    insert #a01_funinfo select 1002   ,     4     ,   '扩展规格' 
    insert #a01_funinfo select 1002    ,    5    ,    '商品品牌' 
    insert #a01_funinfo select 1002     ,   6   ,     '商品信息' 
    insert #a01_funinfo select 1002      ,  7  ,      '商户信息' 
    insert #a01_funinfo select 1002       , 8 ,       '客户信息' 
    insert #a01_funinfo select 1002        ,9,        '数据词典'
    go
    select code = case a01code when 0 then a00code else a01code end
      ,a00name
    from (
      select a00code, a01code=0, a00name from #a00_Module
      union 
      select a00code, a01code, a01name from #a01_funinfo
    ) as a
    order by a00code, a01codedrop table #a01_funinfo, #a00_Module
    -- code a00name
    -- 1001 系统管理
    -- 1 初始设置
    -- 2 权限管理
    -- 1002 基础信息
    -- 3 商品类别
    -- 4 扩展规格
    -- 5 商品品牌
    -- 6 商品信息
    -- 7 商户信息
    -- 8 客户信息
    -- 9 数据词典
    -- 2001 订单管理
    -- 2002 采购管理
    -- 2003 配货管理
    -- 2004 报表管理
    -- 
      

  7.   

    sql server中子查询,oracle中就更好说了,有关键字
      

  8.   

    从所需结果看,其实没必要做join, union后适当的输出排序即可。