表1:
号码  姓名  部门。。
11    aa     mmm
22    bb     nnn
表2:
号码   通话时间  费用
11               10
11               1如何把表2中的表1中没有的号码插入到表1中,而且把插入的号码对应的部门和姓名都改为“无”。

解决方案 »

  1.   

    我用的是本地access数据库。select distinct 主叫号码 from cost where 主叫号码 not in (select 号码 from yh)
    这条语句执行特别慢。
      

  2.   

    select 号码,姓名,部门 into table1(号码,姓名,部门) from table2 where 号码 not in(select 号码 from table1)然后update部门,姓名就好了。
      

  3.   

    insert into 表1(号码) select 号码 from 表2 where 表1.号码 not in(select 号码 from 表2)
    update 表1 set 姓名='无',部门='无'where 姓名=''
      

  4.   

    insert into 表1 select distinct 号码, '无' as 姓名,'无' as 部门 from 表2 where 表2.号码 not in (select 号码 from 表1)
      

  5.   

    Insert into 表1 (号码,姓名,部门)  
    Exec ('Select Distinct 号码,null,null From 表2 where 号码 not In (Select 号码 from 表1 )')
    以上代码在SQLServer测试。Acess试一下。
    表1:
    号码  姓名  部门。。
    11    aa     mmm
    22    bb     nnn
    表2:
    号码   通话时间  费用
    11               10
    44               1
    55              1
    插入后
    表1:
    号码  姓名  部门。。
    11    aa     mmm
    22    bb     nnn
    44
    55
      

  6.   

    Insert into 表1 (号码,姓名,部门)  
    Exec ('Select Distinct 号码,'无','无' From 表2 where 号码 not In (Select 号码 from 表1 )')
    插入后
    表1:
    号码  姓名  部门。。
    11    aa     mmm
    22    bb     nnn
    44    无     无
    55    无     无
      

  7.   

    readersm68(地主)你的意思对,可我没用过exec,改怎么写?
      

  8.   

    select distinct 主叫号码 from cost where 主叫号码 not in (select 号码 from yh)
    为何这条语句执行特别慢?(access)
      

  9.   

    因为每执行一条select distinct 主叫号码 from cost 就要取yh中的每一条记录进行比较 就是说要遍历yh表 当然比较慢
      

  10.   

    Insert into 表1 (号码,姓名,部门)  
    select 号码,'无','无' 
    From 表2 as a
    left join 表1  as b on a.号码=b.号码
    where b.号码 is null
      

  11.   

    改为如下:
    Insert into 表1 (号码,姓名,部门)  
    select a.号码,'无','无' 
    From 表2 as a
    left join 表1  as b on a.号码=b.号码
    where b.号码 is null
      

  12.   

    Insert Into 表1(号码,姓名,部门) select distinct 号码,'无','无' from 表2 where 号码 not in (select 号码 from 表1)
      

  13.   

    select distinct const.code,yh.name,yh.dept into temptbl from yh right join const on yh.code=const.codedelete from yhinsert into yh select * from temptbldrop table temptbl
    慢就没法了,只能分开执行上面的4条语句
      

  14.   

    我测试了用户表500记录,const表10000记录其中有不重复用户2000,每人5条记录用not in 语句c4 1.8,256M DDR 用了半分钟,上面4条语句的时间加起来没有超过2秒,不过,这个因为是分开执行的而且有delete from这样的语句,所以要注意点了
      

  15.   

    select distinct const.code,yh.name,yh.dept into dd from yh right join const on yh.code=const.codeinsert into yh(code)  select code from dd where name is null------那就这样试试