Combining insert and select to implement the method of "inserting the maximum value of a field in the database + 1"

Combining insert and select to implement the method of "inserting the maximum value of a field in the database + 1"

This article is 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 in Table 2 into Table 1. The columns in Table 1 correspond to the columns selected in Table 2.

INSERT INTO table1 (column1, column2, column3, column4)
select column1, column2, column3, column4 from table 2

When inserting data into the table in question 2, a field takes the maximum value of the field in the database and adds 1.

When inserting data into a table, a field takes the maximum value of the field in the database and adds 1. Many people say that this problem can be solved by using stored procedures or something. In fact, the combination of insert and select can solve this problem very well.

Example: When inserting a new record into table 1, the age value is the maximum value in the database and added 1, and is stored in the database as the age of the new data. (Of course, the select statement for age can also add a where condition.)

INSERT INTO table1(
      column1, 
      column2, 
      column3, 
      column4,
      age
    )SELECT
      column1, 
      column2, 
      column3, 
      column4,
      (select IFNULL((select max(age) from table name,0)+1);

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • The difference between MySQL database stored procedures and transactions
  • The meaning and calculation method of QPS and TPS of MySQL database
  • How to backup and restore the mysql database if it is too large
  • PHP backend source code example for backing up MySQL database
  • How to use shell scripts to automatically back up multiple MySQL databases every day
  • Introduction to using mysqli's prepare to operate the database in PHP5
  • MySQL database migration quickly exports and imports large amounts of data
  • Shell script to operate MySQL database to delete duplicate data
  • How to add, delete and modify columns in MySQL database
  • Talk about some experience in database field design

<<:  Docker deployment springboot project example analysis

>>:  How to use webSocket to update real-time weather in Vue

Recommend

Several ways to submit HTML forms_PowerNode Java Academy

Method 1: Submit via the submit button <!DOCTY...

Element-ui directly clicks on the cell in the table to edit

Table of contents Achieve results Implementation ...

MySQL master-slave principle and configuration details

MySQL master-slave configuration and principle, f...

CSS Transition expands and collapses elements by changing the Height

A common development need is that we want to coll...

Detailed explanation of the general steps for SQL statement optimization

Preface This article mainly shares with you the g...

Detailed explanation of Vue3 life cycle functions and methods

1. Overview The so-called life cycle function is ...

Use image to submit the form instead of using button to submit the form

Copy code The code is as follows: <form method...

Understanding of web design layout

<br />A contradiction arises. In small works...

Django online deployment method of Apache

environment: 1. Windows Server 2016 Datacenter 64...

Example of how to implement underline effects using Css and JS

This article mainly describes two kinds of underl...

uniapp realizes the recording upload function

Table of contents uni-app Introduction HTML part ...

How to use vue filter

Table of contents Overview Defining filters Use o...

MySQL data analysis storage engine example explanation

Table of contents 1. Introduce cases 2. View the ...

MySQL 8.0 New Features - Introduction to the Use of Management Port

Table of contents Preface Connection Management A...