开发者

How to update records whose value is not in the ranges stored in another table

开发者 https://www.devze.com 2023-01-28 12:31 出处:网络
I need to update all records in a table for which it\'s value is not part of any range stored in another table. The query that updates records where it\'s value is part of a range is no problem:

I need to update all records in a table for which it's value is not part of any range stored in another table. The query that updates records where it's value is part of a range is no problem:

update table_a
join table_range on
(table_a.x >= table_range.fromValue
and table_a.x <= table_range.toValue开发者_如何学C)
set table_a.someColumn = 'is in range'

But I can't figure out how to update the records of the following query result:

select *
from table_a
where x NOT IN (
select x
from table_a inner join table_range on
(x >= fromValue AND x <= toValue)
)

Thanks in advance


How about the following?

 drop table table_a;
 drop table table_ranges;

 create table table_a(
    x    int        not null
   ,flag varchar(20)
 );

 create table table_ranges(
    fromvalue int not null
    ,tovalue  int not null
 );

 insert into table_a(x) values(10);
 insert into table_a(x) values(20);
 insert into table_a(x) values(30);

 insert into table_ranges values(0,9);
 insert into table_ranges values(10,19);
 insert into table_ranges values(20,29);


 update table_a a
    set flag = 'not in range'
  where not exists(
     select *
       from table_ranges r
      where a.x between r.fromvalue and r.tovalue
  );
0

精彩评论

暂无评论...
验证码 换一张
取 消