insert and select are combined to realize the method of 'Insert the maximum value of a field in the database +1'
- 2021-11-14 07:22:41
- OfStack
This article is an 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, whose columns correspond to the columns from Table 2 select
INSERT INTO Table 1 ( column1, column2, column3, column4 )
select column1, column2, column3, column4 from Table 2
Question 2 When inserting data into the table, a 1 field takes the maximum value of the field in the database and +1,
When inserting data into the table, a 1 field takes the maximum value of the field in the database and +1. Many people say that this problem can be solved by using stored procedures. In fact, the combination of insert and select can solve this problem well.
Example: Table 1 inserts a new record, age value takes the maximum value in the database and +1, and stores it in the database as age of the new data. (Of course, the select statement of age can also add where condition.)
INSERT INTO Table 1(
column1,
column2,
column3,
column4,
age
)SELECT
column1,
column2,
column3,
column4,
(select IFNULL((select max(age) from Table name ,0)+1);
Summarize