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

A Brief Analysis of CSS Selector Grouping

Selector Grouping Suppose you want both the h2 el...

How to solve the problem of blurry small icons on mobile devices

Preface Previously, I talked about the problem of...

Introduction to JavaScript array deduplication and flattening functions

Table of contents 1. Array flattening (also known...

CSS and JS to achieve romantic meteor shower animation

1. Rendering 2. Source code HTML < body > &...

How to upgrade https under Nginx

Purchase Certificate You can purchase it from Ali...

Summary of some small issues about MySQL auto-increment ID

The following questions are all based on the Inno...

XHTML Getting Started Tutorial: XHTML Web Page Image Application

<br />Adding pictures reasonably can make a ...

Solution to changing the data storage location of the database in MySQL 5.7

As the data stored in the MySQL database graduall...

JavaScript implements draggable progress bar

This article shares the specific code of JavaScri...

What are the differences between var let const in JavaScript

Table of contents 1. Repeated declaration 1.1 var...

How to restore docker container data

The project test environment database data is los...

Solution to the failure of loading dynamic library when Linux program is running

Unable to load dynamic library under Linux When t...

MySQL 5.7.13 installation and configuration method graphic tutorial on Mac

MySQL 5.7.13 installation tutorial for Mac, very ...