Empty IN clause parameter list in MySQL

后端 未结 9 1035
攒了一身酷
攒了一身酷 2020-11-30 08:45

What happens when you do a SQL query where the IN clause is empty?

For example:

SELECT user WHERE id IN ();

Will MySQL

9条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 08:51

    Here is another variation of always false statement for empty lists that preserves both logic and the notion of actual column in the query.

    Incorrect id in () can be rewritten into:

    where id <> id;
    

    Similarly incorrect negative condition id not in () can be transformed by custom query building code into:

    where id = id;
    

    This approach is safer than id not in (NULL) as it doesn't evaluate to NULL.

    But beware that this would filter out of the result the rows where id is null. This may be considered a feature in some cases.

    Espesially useful with complex stacked query building logic where nested builder is not aware how the resulting subquery could be used above.

提交回复
热议问题