Example of LAST_INSERT_ID in MySQL

Source: Internet
Author: User

Example of LAST_INSERT_ID usage of MySQL environment: MySQL Sever 5.1 + MySQL command line tool first let's look at an example (the primary key is auto-incrementing): [SQL] mysql> insert into bankaccount (name, balance) values ('20140901', 123); Query OK, 1 row affected (1000 sec) mysql> insert into bankstatement (action, txdate, amt, toaccno, fromaccno) values ('20140901', curdate (), 122, 1, 2); Query OK, 1 row affected (1000 sec) www.2cto.com mysql> select last_insert_id (); + ------------------ + | last _ Insert_id () | + ------------------ + | 7 | + ------------------ + 1 row in set (0.00 sec) mysql> select * from bankaccount; + ------- + ------ + --------- + | accno | name | balance | + ------- + ------ + --------- + | 1 | Zhang San | 200 | 2 | Li Si | 900 | 3 | 123 | 1000 | 4 | 123 | 1000 | + ------- + ------ + --------- + 4 rows in set (0.00 sec) www.2cto.com mysql> select * from bankstatement; + ---- + -------------- + ---- -------- + ------ + --------- + ----------- + | Id | action | txdate | amt | toaccno | fromaccno | + ---- + -------------- + ---------- + ------ + --------- + ----------- + | 1 | account opening | | 100 | NULL | 1 | 2 | account opening | 2012-10-14 | 1000 | NULL | 2 | 3 | query account information | 2012-10-14 | 0 | NULL | 2 | 4 | query an account information | 2012-10-14 | 0 | NULL | 1 | 5 | transfer | 2012-10-14 | 100 | 1 | 2 | 6 | 122 | 2012-10-14 | 1000 | 1 | 2 | 7 | 122 | 2012-10-14 | 1000 | 1 | 2 | + ---- + ---------------- + ---------- + ------ + --------- + ----------- + 7 rows in set (0.00 sec) Summary: LAST_INSERT_ID () returns the value of the AUTO_INCREMENT column in The Last INSERT or UPDATE statement.
References:

LAST_INSERT_ID usage of MySQL explain http://www.bkjia.com/database/201210/160986.html at http://www.bkjia.com/database/201206/137028.htmlmysql#:last_insert_id ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.