--方法(一):分组排练进行更新
----------------------------------------------------------------------------------------------------------------
create table a(id int identity,aid varchar(),name varchar())
insert into a select , 'a '
insert into a select , 'a '
insert into a select , 'a '
insert into a select , 'b '
insert into a select , 'b '
insert into a select , 'c '
insert into a select , 'a '
insert into a select , 'a ' create table b(id int identity,aid varchar(),name varchar())
insert into b select , 'a '
insert into b select , 'a '
insert into b select , 'b '
insert into b select , 'b '
insert into b select , 'b '
insert into b select , 'd ' select px=(select count() from a where aid = t.aid and name= t.name and id<t.id)+,* into #tb1 from a t
select px=(select count () from b where aid = t.aid and name= t.name and id<t.id)+,* 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=)
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(, )
insert into tb1 values(, )
insert into tb1 values(, )
insert into tb1 values(, )
insert into tb1 values(, )
insert into tb1 values(, )
insert into tb1 values(, )
insert into tb1 values(, ) create table tb2(ID int,Account int)
insert into tb2 values(, )
insert into tb2 values(, )
insert into tb2 values(, )
insert into tb2 values(, )
insert into tb2 values(, )
insert into tb2 values(, )
insert into tb2 values(, )
insert into tb2 values(, ) select px=identity(int, ,),* into #t1 from tb1 order by account,id
select px=identity(int ,,),* 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

[SQL]分组排练进行更新的更多相关文章

  1. 取SQL分组中的某几行数据

    取SQL分组中的某几行数据 对表中数据分组,有时只需要某列的聚合值:有时却需要返回整行数据,常用的方法有:子查询.ROW_NUMBER.APPLY,总体感觉还是ROW_NUMBER比较直观.测试数据: ...

  2. sql 分组后显示每组的前几条记录

    sql 分组后显示每组的前几条记录 如   表中记录是             code       serialno             A1               1           ...

  3. SQL分组查询及聚集函数的使用

    今天要做一个查询统计功能,一开始有点犯难,上午尝试大半天才写出统计sql语句,才发现自己sql分组查询及聚集函数没学好:其实就是group by子句和几个聚集函数,熟练使用统计功能很简单.在此总结下今 ...

  4. SQL批量添加,更新,删除

    SQL语句: 存储过程: 代码: 这里说的是关于SQL的批量操作数据. 需要准备: 三层框架:利用 动软代码生成器(可以快速生成增删改查) 生成三层 Model,BLL,Dal ①DbHelpeSQL ...

  5. SQL中使用UPDATE更新数据时一定要记得WHERE子句

    我们在使用 SQL 中的 UPDATE 更新数据时,一般都不会更新表中的左右数据,所以我们更新的数据的 SQL 语句中会带有 WHERE 子句,如果没有WHERE子句,就回更新表中所有的数据,在 my ...

  6. 数据库:sql 多表联合更新【转】

    SQL Update多表联合更新的方法 (1) sqlite 多表更新方法 update t1 set col1=t2.col1 from table1 t1 inner join table2 t2 ...

  7. Mysql 一条SQL语句实现批量更新数据,update结合case、when和then的使用案例

    如何用一条sql语句实现批量更新?mysql并没有提供直接的方法来实现批量更新,但是可以用点小技巧来实现. 复制代码 代码如下: UPDATE mytable SET myfield = CASE i ...

  8. sql 分组后按时间降序排列再取出每组的第一条记录

    原文:sql 分组后按时间降序排列再取出每组的第一条记录 竞价记录表: Aid 为竞拍车辆ID,uid为参与竞价人员ID,BidTime为参与竞拍时间 查询出表中某人参与的所有车辆的最新的一条的竞价记 ...

  9. sql查询,如果有更新时间则按更新时间倒序,没有则按创建时间倒序排列

    原文:sql查询,如果有更新时间则按更新时间倒序,没有则按创建时间倒序排列 ORDER BY IFNULL(update_time,create_time) DESC IFNULL(expr1,exp ...

随机推荐

  1. javascript 毫秒转日期 日期时间转毫秒

    js毫秒时间转换成日期时间 var oldTime = (new Date("2011/11/11 20:10:10")).getTime(); //得到毫秒数 大多数是用毫秒数除 ...

  2. discuz X2.0教程]教你快速了解Discuz!程序文件功能,修改文件从此不用再求人

    x3.x数据字典 http://faq.comsenz.com/library/database/x3/x3_index.htm 先从根目录开始,根目录文件一般都是入口,即执行具体功能的代码一般不在这 ...

  3. FastJson的基本用法----转

    1. [文件] User.java ~ 243B   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class User{     private int id;    ...

  4. SQL SERVER 拆分列为多行

    --创建测试表 )) insert into #temp(names) values('张三,李四'), ('中国,美国,巴西'), ('深圳,上海,北京,广州,哈尔滨'), ('足球,篮球,乒乓球, ...

  5. ubuntu 创建快捷方式

    sudo ln -s /opt/eclipse/eclipse /usr/bin/eclipse 这样就可以在命令行中敲:eclipse,来打开eclipse了. 应该还有一种方式,就像快捷方式一样. ...

  6. Java文件读写

    import java.io.*; public class Study { public static void main(String[] args) { try { String strPath ...

  7. (转)[SQL Server] 动态sql给变量赋值(或返回值给变量)

    本文转载自:http://blog.csdn.net/xiaoxu0123/article/details/5684680 [SQL Server] 动态sql给变量赋值(或返回值给变量) decla ...

  8. Maven 和 Ant 的区别?

    Maven 和 Ant 有什么不同呢?在回答这个问题以前,首先要强调一点:Maven 和 Ant 针对构建问题的两个不同方面.Ant 为 Java 技术开发项目提供跨平台构建任务.Maven 本身描述 ...

  9. PHP替换,只替换匹配到的第一个

    function str_replace_limit($search, $replace, $subject, $limit=-1) { if (is_array($search)) { foreac ...

  10. android学习笔记30——AndroidMainfest.xml

    Manifest.xml文件的职责:指定APP的包名.声明四大组件, 以及启动方式.指定APP运行的进程名称.指定APP权限.指定最小API版本.指定需要连接的库. Manifest.xml的格式:& ...