Detailed Explanation of mysql exists and not exists Examples

  • 2021-08-31 09:34:12
  • OfStack

Detailed Explanation of mysql exists and not exists Examples

tableA

|column1 | column1 |column3 |

tableb

|column1 | column1 |column3 |

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

That is, to get an effect similar to the following statement (not in effect is not exactly the same as not exists, if there is an empty record in the subquery, the whole query statement will not return data)


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

You can do this using the following statement


SELECT
  a.*
FROM
  tableA a
WHERE
  NOT EXISTS(
    SELECT b.column2 FROM tableB b WHERE a.colunm1=b.column2
  )

The above is only two tables, in fact, in the join query of multiple tables is also relatively easy to use. The above writing is also applicable to exists

The above is the detailed explanation of mysql exists and not exists examples. If you have any questions, please leave a message or exchange and discuss in this site community. Thank you for reading. I hope I can help you. Thank you for your support to this site!


Related articles: