1
我想弄清楚如何在影響兩個表的SQL中進行復雜的更新。基於另一個表中的值的SQL if/else更新表
我的2代表的樣子:
t1: key, val_1 (with key as the primary key)
t2: t1_key, user_id, val_2 (with t1_key and user_id as the primary key)
我需要弄清楚是怎麼做它說的更新,給定一個user_ID的 「U」 和一鍵 「K」:
if (["u"+"k"] does not exist at all in t2) {
update t1.val = t1.val+1 where key="k";
insert ("u","k",1) into t2;
} else if (["u"+"k"] exists in t2 and val_2 < 1) {
update t1.val = t1.val+1 where key="k";
update t2.val2 = t2.val2+1 where t1_key="k" AND user_id="u";
}
有什麼建議嗎?
您正在使用哪種RDBMS? –
Microsoft SQL服務器 – Korra
可怕的問題:) –