MYSQL - Difference between IN and EXIST

后端 未结 2 969
慢半拍i
慢半拍i 2020-12-03 06:24

MySql question:

What is the difference between [NOT] IN and [NOT] EXIST when doing subqueries in MySql.

2条回答
  •  没有蜡笔的小新
    2020-12-03 06:38

    They work very differently:

    • EXISTS takes a single argument which should be a subquery (derived table) and checks if there is at least one row returned by the subquery.
    • IN takes two arguments, the first of which should be a single value (or a tuple), and the second of which is a subquery or a tuple and checks if the first value is contained in second.

    However both can be used to check if a row in table A has a matching row in table B. Unless you are careful and know what you are doing I would stay clear of IN in MySQL as it often gives much poorer performance on more complex queries. Use NOT EXISTS or a LEFT JOIN ... WHERE ... IS NULL.

提交回复
热议问题