Use SQL statement to determine whether the record already exists before insert

Use SQL statement to determine whether the record already exists before insert

Determine whether a record already exists before inserting a SQL statement

INSERT INTO test(A,B) select 'ab',2 
 WHERE NOT EXISTS (SELECT * FROM test WHERE A='ab');

Simple collection of judgments when inserting sql

When the user module or other modules require database uniqueness, you need to first determine whether the data already exists in the database before inserting it;

This is the most basic SQL insert statement.

```sql

```sql
INSERT INTO user(name,password) values(admin,123456)

Next, transform it into inserting to determine whether #{parameter} exists

```sql
INSERT INTO user(name, password)
SELECT #{admin},#{123456}
FROM DUAL WHERE NOT EXISTS
(SELECT name,password
FROM user WHERE name= #{admin} and password= # {123456});
``

This is done, the query return value = 0 means failure = 1 means success

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • How to check if data exists before inserting in mysql
  • How to implement inserting a record when it does not exist and updating it if it exists in mysql
  • SQL insert into statement writing method explanation
  • MySQL Practical Experience of Using Insert Statement

<<:  The most comprehensive collection of front-end interview questions

>>:  How to draw a vertical line between two div tags in HTML

Recommend

Native JS realizes uniform motion of various sports

This article shares with you a uniform motion imp...

Example of how to set div background transparent

There are two common ways to make div background ...

Detailed explanation of incompatible changes in rendering functions in Vue3

Table of contents Rendering API changes Render fu...

JavaScript implementation of the back to top button example

This article shares the specific code for JavaScr...

Javascript closure usage scenario principle detailed

Table of contents 1. Closure 2. Closure usage sce...

Why MySQL chooses Repeatable Read as the default isolation level

Table of contents Oracle Isolation Levels MySQL I...

How to monitor oracle database using zabbix agent2

Overview In zabbix version 5.0 and above, a new f...

v-for directive in vue completes list rendering

Table of contents 1. List traversal 2. The role o...

How to customize Docker images using Dockerfile

Customizing images using Dockerfile Image customi...

How to use the VS2022 remote debugging tool

Sometimes you need to debug remotely in a server ...

Specific use of MySQL segmentation function substring()

There are four main MySQL string interception fun...

How to use Vue cache function

Table of contents Cache function in vue2 Transfor...

js to upload pictures to the server

This article example shares the specific code of ...