第一张表:
ID        Name     Sex
100      张三1      true
100      张三2      true
100      张三3      true
210      李四1      false
210      李四2      false
第二张表:
ID   Salary
100  5000
100  6000
100  7000
210  8000
210  9000
我想组合成:
ID    Name  Sex  Salary
100   张三1    true   5000
100   张三2    true   6000
100   张三3    true   7000
210   李四1    false   8000
210   李四2    false   9000
这个sql语句怎么写啊?SQL ServerSQLsql语句

解决方案 »

  1.   


    create table 第一张表
    (ID int, Name varchar(10), Sex varchar(10))insert into 第一张表
     select 100, '张三1', 'true' union all
     select 100, '张三2', 'true' union all
     select 100, '张三3', 'true' union all
     select 210, '李四1', 'false' union all
     select 210, '李四2', 'false'
     
    create table 第二张表
    (ID int, Salary int)insert into 第二张表
     select 100, 5000 union all
     select 100, 6000 union all
     select 100, 7000 union all
     select 210, 8000 union all
     select 210, 9000
     select t1.ID, t1.name, t1.Sex, t2.Salary
    from
    (select ID,name,Sex,
            row_number() over(order by (select 0)) 'rn'
     from 第一张表) t1
    inner join
    (select ID,Salary,
            row_number() over(order by (select 0)) 'rn'
     from 第二张表) t2 on t1.rn = t2.rn/*
    ID          name       Sex        Salary
    ----------- ---------- ---------- -----------
    100         张三1        true       5000
    100         张三2        true       6000
    100         张三3        true       7000
    210         李四1        false      8000
    210         李四2        false      9000(5 row(s) affected)
    */