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 ')