1

2 つのテーブルに影響を与える複雑な更新を 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";
  }

何か案は?

4

1 に答える 1