Suppose the database test table has 3 fields
id(自增)nameorder
Now insert a piece of data to make the order and ID values equal
I'm inserting a piece of data first.
Get to the inserted ID again
The update then sets the order to the ID value
There is no way to insert the ID and order equal
First select MAX (ID) from test
Perform the insert again?
Does this have to take two statements to get it done?
Reply content:
Suppose the database test table has 3 fields
id(自增)nameorder
Now insert a piece of data to make the order and ID values equal
I'm inserting a piece of data first.
Get to the inserted ID again
The update then sets the order to the ID value
There is no way to insert the ID and order equal
First select MAX (ID) from test
Perform the insert again?
Does this have to take two statements to get it done?
Your design is unreasonable, you can see that the order is used as a sort of field, and you want to have the room for manual adjustment, rather than simple design to sort by the primary key, then you can use it directly in the Select to order by `order`, `id`
solve the problem, In this case, the Order field should have a default value, for example 10000, prioritize this field and sort by ID
And of course, remember to add the index
Off-topic, do not use the MySQL keyword as the field name, that is, order this type of name, proposed to change to a priority such as the name!
Can be implemented with MySQL's trigger
You can also use transactions to handle
Is it better to get the last inserted ID SET @lastid = LAST_INSERT_ID();
select max(id) from test
?
Think of a very wretched way is to set order also self-increment.
Why do you use two fields for a value?
Many of the answers upstairs are poorly thought out.
A table cannot set two self-increment fields.
The current generated ID cannot be obtained in the insert process, and can only be done with two statements. Two operations what's the problem?