0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

MariaDBでアップサート(ON DUPLICATE KEY UPDATE)

Last updated at Posted at 2024-07-16

概要

  • 存在していればアップデート。
  • 非存在ならインサート。

1回のSQL発行 で行う方法です。

テーブル

こんなふうな主キーやカラムの設定。
image.png

アップサートするSQL

INSERT INTO unko 
(key_1, key_2, value_1)
VALUES
('あ', 'い', 9)
ON DUPLICATE KEY UPDATE
value_1 = value_1 + 9

実行結果

レコードが無い状態でSQL流したら下記。
image.png

さらにもう一回SQL流したら下記。
image.png

参考サイトさん

バージョン

Microsoft Windows [Version 10.0.22631.3880]
MariaDB 10.5.4

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?