日期:2014-05-17  浏览次数:20959 次

怎么Update 符合两个字段的 批量数据
create table Test
(
  ID NUMBER(20) not null,
  TYPE INTEGER not null,
  INLIST INTEGER default 0,
  DATE DATE not null,
)
ID和 Type是主键。

例如有如下数据。
1, 1, 0, 2012-8-13 1:25:00 一行
2, 1, 0, 2012-8-13 2:25:00 二行
3, 1, 0, 2012-8-13 3:25:00 三行
3, 2, 0, 2012-8-13 4:25:00 四行
4, 1, 0, 2012-8-13 5:25:00 五行
4, 2, 0, 2012-8-13 6:25:00 六行
5, 1, 0, 2012-8-13 7:25:00 七行
6, 1, 0, 2012-8-13 7:25:00 八行
7, 1, 0, 2012-8-13 7:25:00 九行
7, 7, 0, 2012-8-13 7:25:00 十行

根据时间取最前5个,现在把,1行,2行,3行,4行,5行取出后, 

需要更新 INLIST 为1,怎么写这个Update 语句呢
当然可以使用Update Test set INLIST=1 where (id=1 and type=1) or (id=2 and type=1) or (id=3 and type=1) or (id=3 and type=2) or (id=4 and type=1)
这样不方便, 假设有10万数据,我一次去前1000条,然后更新 INLIST=1, 这样的话就需要加上 1000个or (id=2 and type=1)
有什么好的语句么?

------解决方案--------------------
SQL code

update Test set INLIST=1
    where exists(
        select 1 from
        (select row_number() over(partition by Type order by DATE) rn,ID,Type from Test) t
        where Test.ID=t.ID and Test.Type=t.Type and Test.Type=1 and t.rn<=5
    );