简体   繁体   English

更新Oracle表以获取多个记录

[英]Update Oracle table for multiple records

I have couple of tables in my Oracle database as shown below 我的Oracle数据库中有几个表,如下所示

**IM_FP**
FP_ID
FP_NAME
FP_STATUS

**IM_FP_DTL**
FP_DTL_ID
FP_ID
IM_ID
FP_DATE

FP_ID    FP_NAME   FP_STATUS
1         ABC        TRUE
2         DEF        TRUE
3         GHI        TRUE
4         TEC        TRUE
5         KEC        TRUE

FP_DTL_ID         FP_ID   IM_ID     FP_DATE
1                  1        1        1-JAN-1996
2                  2        1       10-JAN-1996
3                  3        1        2-FEB-1996
4                  4        2        1-JAN-1996
5                  5        3        2-JAN-2010

There is a 1-1 relationship between both the tables. 两个表之间存在1-1关系。 But the IM_ID can be duplicates in the FP_DTL table and update the FP_STATUS value in IM_FP table to false for all the records but True for the record with MAX(FP_DTL_ID). 但是,IM_ID可以在FP_DTL表中重复,并且可以将IM_FP表中的FP_STATUS值更新为对所有记录为false,但对具有MAX(FP_DTL_ID)的记录为True。

For example in the above scenario, FP_STATUS in IM_FP table for the first two records will be FALSE and the third record will be TRUE 例如,在上述情况下,IM_FP表中前两个记录的FP_STATUS将为FALSE,第三个记录将为TRUE

Select IM_ID from FP_DTL GROUP BY IM_ID HAVING COUNT(IM_ID)>1

will give me all the AM_ID that are duplicates. 会给我所有重复的AM_ID。

Please help 请帮忙

How about: 怎么样:

UPDATE IM_FP
SET FP_STATUS = 'TRUE'
WHERE FP_ID IN
  (SELECT MAX(FP_DTL_ID)
   FROM   IM_FP_DTL
   GROUP BY IM_ID)
AND (FP_STATUS IS NULL OR FP_STATUS != 'TRUE');

UPDATE IM_FP
SET FP_STATUS = 'FALSE'
WHERE FP_ID NOT IN
  (SELECT MAX(FP_DTL_ID)
   FROM   IM_FP_DTL
   GROUP BY IM_ID)
AND (FP_STATUS IS NULL OR FP_STATUS != 'FALSE');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM