Assume that the database test table has three fields {code ...} now we want to insert a piece of data so that the values of order and id are equal. I want to insert a piece of data first and then get the inserted id. Then, update sets order to the id value. Is there any way to insert it? equal order or selectmax (... assume that the database test table has three fields.
Id (auto-increment) nameorder
Insert a data record to make the values of order and id equal.
Insert a data entry first.
Obtain the inserted id.
Then, update sets order to id.
Is there a way to make the id and order equal when inserting
Select max (id) from test first
Then insert?
In this case, must two statements be used to solve the problem?
Reply content:
Assume that the database test table has three fields.
Id (auto-increment) nameorder
Insert a data record to make the values of order and id equal.
Insert a data entry first.
Obtain the inserted id.
Then, update sets order to id.
Is there a way to make the id and order equal when inserting
Select max (id) from test first
Then insert?
In this case, must two statements be used to solve the problem?
Your design is unreasonable. We can see that your order is used as a sorting field and you want to adjust it manually, instead of simply designing it to sort by the primary key, in this case, you can directly useorder by `order`, `id`
This problem is solved. In this case, the order field can be sorted by a default value, for example, 10000. This field is prioritized and then sorted by id.
Remember to add indexes.
In other words, do not use the keyword of mysql as the field name, that is, the name such as order. We recommend that you change it to a name such as priority!
Mysql triggers can be used for implementation.
You can also use transactions for processing.
Obtain the id of the last insert,SET @lastid = LAST_INSERT_ID();
Is it betterselect max(id) from test
Good?
I thought of a very tricky way to set order as auto-increment.
Why two fields are used for a value?
There are many questions on the top of the page.
Two auto-increment fields cannot be set for a table.
You cannot obtain the id generated during the insert Process. You can only use two statements. Are there any problems with the two operations?