zoukankan      html  css  js  c++  java
  • [SQL]分组排练进行更新

    --方法(一):分组排练进行更新
    ----------------------------------------------------------------------------------------------------------------
    create table a(id int identity,aid varchar(5),name varchar(6))  
    insert into a select 1,  'a  '  
    insert into a select 1,  'a  '  
    insert into a select 1,  'a  '  
    insert into a select 1,  'b  '  
    insert into a select 1,  'b  '  
    insert into a select 1,  'c  '  
    insert into a select 2,  'a  '  
    insert into a select 2,  'a  '  
    
    create table b(id int identity,aid varchar(5),name varchar(6))  
    insert into b select 1,  'a  '  
    insert into b select 1,  'a  '  
    insert into b select 1,  'b  '  
    insert into b select 1,  'b  '  
    insert into b select 1,  'b  '  
    insert into b select 1,  'd  ' 
    
    
    
    select px=(select count(1) from a  where aid = t.aid and name= t.name and id<t.id)+1,* into #tb1 from a t 
    select px=(select count (1) from b where aid = t.aid and name= t.name and id<t.id)+1,* into #tb2 from b t
    
    select * from #tb1
    select * from #tb2
    delete a where id in
    (select id from #tb1 a where not exists(select * from #tb2 b where b.px=a.px and b.aid=a.aid and b.name=a.name)
    and a.aid=1)
    insert into a
    select aid,name from #tb2 a  where not exists(select * from #tb1 b where b.px=a.px and b.aid=a.aid and b.name=a.name)
    
    -------------------------------------------------------------------------------------------------------------------
    --方法(二):分组排练进行更新
    
    create table tb1(ID int,Account int) 
    insert into tb1 values(11,                 10000 ) 
    insert into tb1 values(12,                 10000 ) 
    insert into tb1 values(13,                 10000 ) 
    insert into tb1 values(14,                 20000 ) 
    insert into tb1 values(15,                 25000 ) 
    insert into tb1 values(16,                 35000 ) 
    insert into tb1 values(17,                 40000 ) 
    insert into tb1 values(18,                 50000 )  
    
    create table tb2(ID int,Account int) 
    insert into tb2 values(1,                 10000 ) 
    insert into tb2 values(2,                 10000 ) 
    insert into tb2 values(3,                 20000 ) 
    insert into tb2 values(4,                 20000 ) 
    insert into tb2 values(5,                 20000 ) 
    insert into tb2 values(6,                 30000 ) 
    insert into tb2 values(7,                 40000 ) 
    insert into tb2 values(8,                 80000 )  
    
    select px=identity(int, 1,1),* into #t1 from tb1 order by account,id
    select px=identity(int ,1,1),* into #t2 from tb2 order by account,id
    
    
    select a.id,a.account from #t1 a where not exists(select * from #t2 b where a.px=b.px and a.account=b.account)
    -----------------------------------------------------------------------------------------------------------------
    --删除临时表
    
    drop table tb1,tb2,#t1,#t2
  • 相关阅读:
    Linux Core Dump【转】
    定位多线程内存越界问题实践总结【转】
    gcc __attribute__关键字举例之visibility【转】
    va_start(),va_end()函数应用【转】
    谁在call我-backtrace的实现原理【转】
    利用backtrace和ucontex定位segment错误【转】
    善用backtrace解决大问题【转】
    linux backtrace()详细使用说明,分析Segmentation fault【转】
    利用gcc自带的功能-fstack-protector检测栈溢出及其实现【转】
    Linux环境下段错误的产生原因及调试方法小结【转】
  • 原文地址:https://www.cnblogs.com/beeone/p/3683848.html
Copyright © 2011-2022 走看看