SQL仅在ORACLE中使用in子句更新一个ROW

时间:2018-06-20 08:00:31

标签: sql oracle sql-update

我知道如何使用SQL,但是现在我有一些特定的要求要做,我有点困惑。

我有一个具有documentnumber列的表,这是表的信息:

Documentnumber VIB       Status
1              TAS7002   OK
1              TAS7003   OK
1              TAS7004   OK
2              TAS7002   OK
2              TAS7003   OK
2              TAS7004   OK

我只想更新一行,对于(1,2)中的每个不同文档号。示例可能是: 更新文档编号= 1,并且VIB = TAS7002 &&文档编号= 2,并且vib = TAS7002 可以随机选择应更新的震动编号,可以是其中任何一个(TAS7002,TAS7003等),但是我想将此查询放入一个查询中。原因是我有文档编号作为列表,但没有vib编号...

我尝试过的是:

update bshm_sp_pidok_objlink
set status = 'NEW'
where documentnumber in
(1,2)
and status = 'OK'
and rownum = 1;

您能检查我在做什么吗?

4 个答案:

答案 0 :(得分:4)

您可以使用子查询,该子查询为每个文档编号选择一行:

update bshm_sp_pidok_objlink
  set status = 'NEW'
where (documentnumber, vib) in (select documentnumber, min(vib) 
                                from bshm_sp_pidok_objlink
                                where documentnumber in (1,2)
                                  and status = 'OK'
                                group by documentnumber);

答案 1 :(得分:1)

update bshm_sp_pidok_objlink
set boogle_status = 'FOO'
where (dokar, rowid) in ( 
    select  dokar, min(rowid) 
    from bshm_sp_pidok_objlink
    where dokar in ('ARA','AGA')
           and boogle_status = 'NEW'
    group by dokar
);

答案 2 :(得分:0)

update bshm_sp_pidok_objlink
set status = 'NEW'
where (documentnumber,status,VIB) IN 
    (select documentnumber,status,VIB 
       from bshm_sp_pidok_objlink
     where documentnumber in (1,2)
       and status = 'OK'
       and rownum = 1);

EDIT-1

它将更新每个组中的随机行,尝试一下:

UPDATE bshm_sp_pidok_objlink 
SET 
    status = 'NEW'
WHERE
    rowid IN (SELECT 
            rid
        FROM
            (SELECT 
                documentnumber, MIN(rid) rid
            FROM
                (SELECT 
                documentnumber, FIRST_VALUE(rowid) over (order by DBMS_RANDOM.VALUE) rid
            FROM
                bshm_sp_pidok_objlink
            WHERE
                documentnumber IN (1 , 2)
                    AND status = 'OK')
            GROUP BY documentnumber))

答案 3 :(得分:0)

请使用ROWID来获取唯一ID并更新值。 例如:

Create table aa ( doc_no varchar2(100),  
       docname varchar2(100),status varchar2(100));

Insert into aa values ( '1','VID11111','OK');
Insert into aa values ( '1','VID11112','OK');
Insert into aa values ( '1','VID11113','OK');
Insert into aa values ( '1','VID11114','OK'); 
Insert into aa values ( '1','VID11115','OK');

Insert into aa values ( '2','VID22221','OK');
Insert into aa values ( '2','VID22222','OK');
Insert into aa values ( '2','VID22223','OK');
Insert into aa values ( '2','VID22224','OK'); 
Insert into aa values ( '2','VID22225','OK');

Commit;

Select * from aa  ;
Update aa set status = 'New' 
Where rowid in 
( select min(rowid ) from aa group by doc_no);

关于, 维迪亚D