Example of LAST_INSERT_ID in MySQL _ MySQL

Source: Internet
Author: User
The LAST_INSERT_ID usage example of MySQL is bitsCN. example of LAST_INSERT_ID usage of comMySQL environment: MySQL Sever 5.1 + MySQL command line tool first 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) 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) mysql> select * from bankstatement; + ---- + ---------- ---- + ------------ + ------ + --------- + ----------- + | Id | action | txdate | amt | toaccno | fromaccno | + ---- + -------------- + ---------- + ------ + --------- + ----------- + | 1 | account opening | 2012-10-14 | 100 | NULL | 1 | 2 | account opening | 2012-10-14 | 1000 | NULL | 2 | 3 | query account information | 2012-10-14 | 0 | NULL | 2 | 4 | search for account information | 0 | NULL | 1 | 5 | transfer | 2012-10-14 | 100 | 1 | 2 | 6 | 122 | 2012-10-14 | 100 0 | 1 | 2 | 7 | 122 | 2012-10-14 | 1000 | 1 | 2 | + ---- + -------------- + ------------ + ------ + --------- + ----------- + 7 rows in set (0.00 sec) conclusion: LAST_INSERT_ID () returns the value of the AUTO_INCREMENT column in the last INSERT or UPDATE statement.
References:

MySQL LAST_INSERT_ID usage http: // database/201208/151747 .html; mysql LAST_INSERT_ID () analysis http: // database/201206/137028 .html Mysql function: Last_insert_id () syntax explanation http: /// database/201210/160986 .htmlbitsCN.com

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.