Conversion

LAST_INSERT_ID

MySQLMySQL

Returns the last AUTO_INCREMENT value generated for a column in the current connection.

Tipe hasil: BIGINT UNSIGNEDDiperbarui: 13 Jun 2026

Syntax

SQL
LAST_INSERT_ID([expr])

Parameter

exprBIGINTopsional

Optional: set the value to be returned by subsequent calls

Contoh Penggunaan

Get Last ID

SQL
1INSERT INTO users (name) VALUES ('John');
2SELECT LAST_INSERT_ID() AS new_user_id;

Retrieve the ID of the newly created user.

Hasil
new_user_id: 42

Insert Related Records

SQL
1INSERT INTO orders (customer_id, total) VALUES (1, 100);
2INSERT INTO order_items (order_id, product_id, qty)
3VALUES (LAST_INSERT_ID(), 101, 2);

Insert into a related table using the foreign key from the previous insert.

Hasil
(linked records inserted)

Set Custom ID

SQL
1SELECT LAST_INSERT_ID(1000) AS set_id;
2-- Subsequent calls return 1000
3SELECT LAST_INSERT_ID() AS current_id;

Set a custom value for the session.

Hasil
set_id: 1000, current_id: 1000

Pertanyaan Umum tentang LAST_INSERT_ID

Apa itu fungsi LAST_INSERT_ID di MySQL?
Returns the last AUTO_INCREMENT value generated for a column in the current connection. Di MySQL, fungsi LAST_INSERT_ID termasuk dalam kelompok fungsi yang sering digunakan untuk mengolah dan menganalisis data secara efisien.
Bagaimana cara menggunakan LAST_INSERT_ID di MySQL?
Gunakan sintaks berikut: LAST_INSERT_ID([expr]). Pastikan argumen yang dimasukkan sudah sesuai dengan tipe data yang diharapkan.
Apa nilai yang dikembalikan oleh fungsi LAST_INSERT_ID?
Fungsi LAST_INSERT_ID mengembalikan nilai bertipe BIGINT UNSIGNED. Pastikan tipe data hasil sudah sesuai dengan kebutuhan query atau formula kamu. Jika input mengandung nilai NULL, perilaku fungsi dapat berbeda — selalu periksa dokumentasi untuk memastikan hasilnya sesuai ekspektasi.
Apa saja parameter fungsi LAST_INSERT_ID?
Semua 1 parameter bersifat opsional. Parameter yang digunakan: expr (BIGINT, opsional): Optional: set the value to be returned by subsequent calls.
Ngulik Langsung

Sudah paham LAST_INSERT_ID? Latih langsung di browser

Latihan interaktif, langsung di browser.

Latihan Langsung →