insert table1(id1 = substring(table.id,1,6),name1=table.name,re1=table.re)
select table(id,name,re) form table where table.id not exists
select table1.id form table where table1.id = substring(table.id,1,6)怎么不对呀

解决方案 »

  1.   

    你是什么数据库?mysql好像不支持如此子查询的sql
      

  2.   

    MYSQL应该可以吧?我现在要的语句是用在SQL SERVER 2000上的
    但我的语句好象不对请问怎么改
      

  3.   

    对sql server不太了解。大概是:
    insert into table1(id1,name1,re1) select substring(id,1,6),name,re from table
     where id='xxxxxxx'
    这样。
    如果id1有唯一索引,那么这样就可以了,不会重复插入。如果不唯一,可以用大概如下形式:
    insert into table1(id1,name1,re1) select substring(id,1,6),name,re from table
     where id='xxxxxxx' and not exist 
           (select * from table1 where id1=substring('xxxxxxxx',1,6)  )不太了解sql server,不知道substring函数是否正确,
    另,不知是否有其它更简单形式代替上面的子查询
      

  4.   

    我要查找的id不是唯一的
    可能是一条
    也可能是n条是对整两个数据库进行操作
    有点类似于批处理
      

  5.   

    id 是数字, substring是字符串,两个是不能相等的,用convert函数转换
      

  6.   

    一条就用=
    多个就用 in (  )
    整个表就把条件给去了meteorlet说得对,是要类型转换得
      

  7.   

    --测试declare @t1 table(id varchar(9),name varchar(50),re varchar(20))
    declare @t2 table(id varchar(6),name varchar(50),re varchar(20))--insert t1
    insert @t1 
    select '123456789','aaa',''
    union all
    select '123676789','aaa',''
    union all
    select '123676783','aaa',''select * from @t1insert @t2 select substring(t.id,1,6),name,re from @t1 t where not exists(select 1 from @t2 where id=substring(t.id,1,6))select * from @t2
      

  8.   

    INSERT INTO [table2](id1,name1,re1) SELECT LEFT(id,6) AS nid,name,re FROM [table1] WHERE id NOT IN (SELECT a.id FROM [table1] a INNER JOIN [table2] b ON LEFT(a.id,6)=b.id1)
      

  9.   

    如果table1中有一个bit的字段而table2中对应的没有呢?怎么转?