我试图弄清楚如何在影响两个表的 SQL 中进行复杂的更新。
我的 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";
}
有任何想法吗?