table1:
id,value
1,1111
2,2222
3,4444
4,1111
5,2222table2:
id,table1Id,Table1Value(唯一约束)上面的数据,我想把table1中的数据插入到table2,
table1中value重复的值我只要插入一个
且table2的Table1Value不允许重复值,要怎么来写呢?谢谢

解决方案 »

  1.   

    INSERT  table2
            ( table1Id ,
              Table1Value
            )
            SELECT  table1id ,
                    value
            FROM    table1
            GROUP BY table1id ,
                    value
      

  2.   

    insert into table2(table1Id,Table1Value)
    select min(id) as id,value from 
    table1 group by value
      

  3.   


    insert into table2(table1Id,Table1Value)
    select max(id) as id,value from 
    table1 group by value
      

  4.   

    table2中没数据的话2、3L的方法可以解决,有数据的话还需要在插入前再比较一次
      

  5.   

    2楼的可以,用分组,下面的我用的是rownumber分组,思想差不多。
    insert into table2(table1id,table1value)
    select id,value from 
    (
      select id,value,ROW_NUMBER() over (partition by value order by id) as rownum  from table1
    ) as t
    where rownum<2