LoginSignup
10
13

More than 5 years have passed since last update.

【MySQL】カラムの特定文字を一気にupdate

Last updated at Posted at 2015-09-21

userテーブルのnameカラムの「鈴木」を「佐藤」へ一気にupdateしたい場合。

  • userテーブル
name age
鈴木 太郎 27
鈴木 さとし 35
佐藤 五郎 42
斉藤 まさし 28

updateとreplaceを合わることで一気にupdateできる。

sql
update user
set name = replace(name ,'鈴木','佐藤');

たぶん、whreで条件つけるともっと安全。

sql
update user
set name = replace(name ,'鈴木','佐藤')
where  name like '%鈴木%';

右端から1文字を削除したい

例えば右端に半角スペース入っていて削除したいみたいな場合

sql
update user
set name = left(name,char_length(name)-1)
where name like '% ';

参考:SQL文で右から1文字だけ削除するやり方

10
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
13