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?

More than 5 years have passed since last update.

MySQLで採番する(採番用のテーブルを作る)

Posted at

↓のテーブルを作って、

Create Table: CREATE TABLE `seq` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
)

↓を実行する。

UPDATE seq SET id=LAST_INSERT_ID(id+1);
select LAST_INSERT_ID();

select LAST_INSERT_ID(); で取得する値は接続毎なので、別接続で同じ処理を実行された後、select LAST_INSERT_ID(); しても結果は変わらない。
※別接続の方は、update で更新した新しいIDの値が取得できる。
※別接続でupdateされた後、再度こちらでupdateした場合は、最新のIDからの連番がちゃんと取得できる。

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?