Mysql Insert Record Instert statement detailed

Source: Internet
Author: User
Tags mysql insert

Insert record of Mysql statement Instert

Single line insert

The code is as follows Copy Code

Insert into table name (field name, field name, ...) VALUES (field value, field value,...);

Insert into A (a_e,a_f,a_a) values (' characters ', 10,01);

MultiRow Insert

The code is as follows Copy Code

Insert into Table name values (field value, field value,...), (field value, field value,...);
INSERT into A values (' character ', 10,01), (' character ', 10,01);

Returns the record ID after inserting a record


MySQL provides a last_insert_id () function.

The code is as follows Copy Code

LAST_INSERT_ID () (with no argument) returns the automatically generated value, is set for a auto_increment col Umn by the most recently executed inserts or UPDATE statement to affect such a column. For example, after inserting a row that generates a Auto_increment value, you can get the value like this:

Mysql> SELECT last_insert_id ();
-> 195


Simply put, this function will return the value of the field that the inserted record added to the table, and we generally name the self-added field as an ID. This allows you to return the ID value of the record you just inserted.

A simple example:

The code is as follows Copy Code
$query = "INSERT into ' testtable ' (' clou1 ', ' clou2 ') VALUES (' TestValue ', ' Test ')";
mysql_query ($query);
$query = "Select last_insert_id ()";
$result =mysql_query ($query);
$rows =mysql_fetch_row ($result);
echo $rows [0];

When the MySQL record is not present, the insert record exists the implementation method of the update

The code is as follows Copy Code
mysql> truncate ' 200702 ';
Query OK, 0 rows affected (0.01 sec)
Mysql> select * from ' 200702 ';
Empty Set (0.01 sec)
mysql> insert INTO ' 200702 ' (' Domain ', ' 2nd_domain ', ' TLD ', ' query_ns1 ', ' query_ns2 ', ' report_date ') VALUES (' DNSPOD.C Om ', ' dnspod ', ' com ', 1000, Watts, ' 2007-02-04 ') on DUPLICATE KEY UPDATE ' query_ns1 ' = ' query_ns1 ' + 1000, ' query_ns2 ' = ' q Uery_ns2 ' + 2000;
Query OK, 1 row Affected (0.00 sec)

Of course, when building a table, don't forget to give Domain a unique

UNIQUE KEY ' domain ' (' domain ', ' report_date ')

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.