Detailed explanation of mysql exists and not exists examples

Detailed explanation of mysql exists and not exists examples

Detailed explanation of mysql exists and not exists examples

tableA

|column1 | column1 | column3 |

tableb

|column1 | column1 | column3 |

To query the data of tableA, the condition is that tableA.column1 is not in tableB.column2 of tableB

That is to say, you want to get an effect similar to the following statement (the effect of not in is not completely equivalent to not exists. If there is an empty record in the subquery, the entire query statement will not return data)

SELECT 
  a.*
FROM
  table A
WHERE 
  a.column1 not in (
    SELECT column2 FROM tableB
  )

This can be achieved using the following statement

SELECT
  a.*
FROM
  table A
WHERE
  NOT EXISTS(
    SELECT b.column2 FROM tableB b WHERE a.column1=b.column2
  )

The above is only the case of two tables. In fact, it is also easy to use in the connection query of multiple tables. The above writing method also applies to exists

The above is a detailed example of MySQL exists and not exists. If you have any questions, please leave a message or discuss in the community of this site. Thank you for reading and hope to help everyone. Thank you for your support of this site!

You may also be interested in:
  • SQL statement optimization: replace IN with EXISTS and NOT IN with NOT EXISTS
  • Usage of sqlserver exists, not exists
  • How to write if not exists in mssql and sqlite
  • Subtle differences in the use of SQL not in and not exists
  • Let's talk in detail about the usage of exists and not exists in SQL

<<:  Linux loading vmlinux debugging

>>:  jQuery implements the practice of changing the position and size of div by dragging the mouse

Recommend

Example code for implementing hollowing effect with CSS

Effect principle Mainly use CSS gradient to achie...

Why MySQL does not recommend deleting data

Table of contents Preface InnoDB storage architec...

echars 3D map solution for custom colors of regions

Table of contents question extend Solving the pro...

Several solutions for forgetting the MySQL password

Solution 1 Completely uninstall and delete all da...

Analysis of two usages of the a tag in HTML post request

Two examples of the use of the a tag in HTML post...

Ubuntu 20.04 firewall settings simple tutorial (novice)

Preface In today's increasingly convenient In...

Three ways to copy MySQL tables (summary)

Copy table structure and its data The following s...

Detailed explanation of the use of stat function and stat command in Linux

stat function and stat command Explanation of [in...

How to use vue3 to build a material library

Table of contents Why do we need a material libra...

JavaScript clicks the button to generate a 4-digit random verification code

This article example shares the specific code of ...

What to do if the online MySQL auto-increment ID is exhausted

Table of contents Table definition auto-increment...

Practice using Golang to play with Docker API

Table of contents Installing the SDK Managing loc...