別テーブルのデータを使って更新

TableAの値をTableBの値で更新する時に、色々ハマったのでメモ

複数テーブルを参照するUPDATE文は...

UPDATE TableA, TableB
SET TableA.col1 = TableB.col2
WHERE TableA.key = TableB.colKey;

TableBは更新しないけどSETで関連付けるために書く必要があるみたい、作法みたいなものかな。
あとはSELECT文と同じ感じで。

MySQLWorkBenchで実行してみたら...
エラーになって以下のメッセージが表示されてた。

Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
To disable safe mode, toggle the option in Preferences -> SQL Editor -> Query Editor and reconnect.

オプション変えろと言われているので、指示通りに
メニューのEdit -> Preferences -> SQL Editor -> Query Editor を開き

"Sage Updates", Forbid UPDATEs and DELETEs with no Key in WHERE clause or no LIMIT clause, Requires a reconnection.

のチェックを外して、再接続をしたらうまく更新できた。
要は主キーを使わずにUPDATEやDELETEやるのは危険なので、標準で使えないようになっているという事かな。