Select by varchar column with IN() part in condition and int value returns all rows

后端 未结 4 2040
感情败类
感情败类 2021-01-24 02:14

Can someone explain me why with that query:

SELECT * FROM `tags` WHERE (tag IN (\'willa-lentza\', 2016))

it return me all rows from tags<

4条回答
  •  太阳男子
    2021-01-24 02:33

    I cannot reproduce this behaviour, but it seems that your varchars get casted into a DOUBLEs and not the other way around.

    In this case, the query turns into this:

    SELECT  *
    FROM    tags
    WHERE   CAST(tag AS DOUBLE) /* =0 for non-numeric tags */ IN (CAST('willa-lentza AS DOUBLE) /* = 0 */, 2016)
    

    which is always true for all non-numeric tags.

    Could you please run EXPLAIN EXTENDED SELECT ... on this statement and post the warning here?

    To confirm this behavior, you can add another numerical tag:

    INSERT
    INTO    tags
    VALUES  (6, '1000')
    

    This tag should be returned by neither of the queries.

    To avoid this, just always enclose your constants into single quotes so that they are parsed as CHARs

提交回复
热议问题