[英]Updating multiple columns in a single sql query with conditions
在為以下條件構建sql查詢時,我需要幫助:
oracle數據庫中有3列,我需要更新這些列之一,以檢查1st是否為null,然后更新該列,並且不要觸摸其他兩列,如果1st不為null,則更新2nd,並且不更新第三列否則,如果第三列為空,則更新第三列。
我可以建立的查詢是:
update temp set
flg1 =
case flg1
when null then 'Y' else flg1 end,
flg2 =
case flg2
when null then 'Y' else flg2 end,
flg3 =
case flg3
when null then 'Y' else flg3 end,
where id = 132
我知道以上查詢與我想要的不同,需要幫助..
嘗試這個:
update temp set
flg1 =
case when flag1 is null then 'Y' else flg1 end,
flg2 =
case when flag1 is not null and flag2 is null then 'Y' else flg2 end,
flg3 =
case when flag1 is not null and flag2 is not null and flag3 is null then 'Y' else flg3 end
where id = 132
您應該使用嵌套大小寫條件來滿足您的要求,只需嘗試以下代碼
update temp set
flg1 =
case when flag1 is null then 'Y' else flg1 end,
flg2 =
case when flag1 is not null
then case flag2 is null then 'Y' else flg2 end
end,
flg3 =
case when flag1 is not null
then case flag2 is not null
then case flag3 is null then 'Y'
else flg3 end
end
end
where id = 132
也許您想要這樣的東西?
UPDATE temp SET
flg1 =
CASE WHEN flg1 IS NULL THEN 'Y' ELSE flg1 END,
flg2 =
CASE WHEN flg1 IS NOT NULL AND flg2 IS NULL THEN 'Y' ELSE flg2 END,
flg3 =
CASE WHEN flg1 IS NOT NULL AND flg2 IS NOT NULL AND flg3 IS NULL THEN 'Y' ELSE flg3 END
WHERE id = 132
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.