How to implement insert and select & quot; inserts the maximum value of a field in the database + 1"
This article mainly introduces insert and select how to achieve "insert a field in the database of the maximum value + 1", has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
This article is a mysql database
Question 1: import the data from Table 1 into Table 2:
Import the data from Table 1 into Table 2:
Insert the data from Table 2 into Table 1, and the columns in Table 1 correspond to the columns from Table 2 select
INSERT INTO Table 1 (column1, column2, column3, column4) select column1, column2, column3, column4 from Table 2
When data is inserted into the table of question 2, a field takes the maximum value of the field in the database, and + 1
When inserting data in the table, a certain field takes the maximum value of the field in the database and + 1. Many people say that stored procedures can be used to solve this problem. In fact, the combination of insert and select can solve this problem very well.
Example: table 1 inserts a new record, and the age value takes the maximum value in the database and + 1, which is stored in the database as the age of the new data. (of course, the select statement of age can also add where conditions. )
INSERT INTO Table 1 (column1, column2, column3, column4, age) SELECT column1, column2, column3, column4, (select IFNULL ((select max (age) from Table name, 0) + 1) Thank you for reading this article carefully. I hope the article "how to achieve insert and select" and "insert the maximum value of a field in the database + 1" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!