Quantcast
Channel: Layer8 » MySQL
Viewing all articles
Browse latest Browse all 20

MySQLで最後にINSERT(挿入)した行のIDを知りたい

$
0
0

LAST_INSERT_ID()という関数を用います。まずはCategoryにInsertします。

mysql> INSERT INTO Category (name,parent_id,article_num) VALUES('Linux',107,0);
Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM Category;
+----+--------+-----------+-------------+
| id | name   | parent_id | article_num |
+----+--------+-----------+-------------+
|  1 | PHP    |       100 |           1 |
|  2 | MySQL  |       102 |           1 |
|  3 | Apache |       103 |           2 |
|  4 | Linux  |       107 |           0 |
+----+--------+-----------+-------------+
4 rows in set (0.00 sec)

mysql> SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
|                4 |
+------------------+
1 row in set (0.00 sec)

Viewing all articles
Browse latest Browse all 20

Latest Images

Trending Articles





Latest Images