24
25

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

mysql 複数行のupdate

Posted at

大量にUPDATEしたい

場合に


update work_shops set point = 10 where id = 1;
update work_shops set point = 20 where id = 2;
update work_shops set point = 30 where id = 3;
update work_shops set point = 40 where id = 4;
update work_shops set point = 50 where id = 5;
update work_shops set point = 60 where id = 6;
update work_shops set point = 70 where id = 7;

こんなのを何行も書いてやってましたのでまー時間がかかっちゃいますね。

INSERT ... ON DUPLICATE KEY UPDATE 構文

を使ったらいいと。

まずはふつうにINSERT文を作る


INSERT INTO work_shops (id, point) VALUES
(1,10),
(2,20),
(3,30),
(4,40),
(5,50),
(6,60),
(7,70)


ON DUPLICATE KEY UPDATE point = VALUES(`point`);

更新したい値を指定。

でOK。

id はプライマリーキー

work_shops.id に 同じキーが
なければINSERT。
あればUPDATE。

される。

OK。

24
25
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
24
25

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?