奇怪的MySQL IN行为

时间:2017-03-31 08:03:15

标签: mysql

好的,这不是100%的问题。 今天我很头疼,只是想知道是否有其他人有这个问题。

假设我们有一个名为“products”的表:

ID | product_code
4  | 5201279007942_AKYRO

如果我们运行此查询:

SELECT *
FROM `products`
WHERE product_code
IN ( '5201279007942' )

Mysql将按预期返回0行。

如果我们运行此查询:

SELECT *
FROM `products`
WHERE product_code
IN ( 5201279007942 )

Mysql将返回上面的行。

因此“IN”查询将充当“LIKE”查询。

这种行为是正常的吗?我在这里错过了什么吗?

1 个答案:

答案 0 :(得分:1)

查看输出。 string ant int

之间存在差异
SELECT 
    '5201279007942_AKYRO' IN ('5201279007942') AS string_with_string
    , '5201279007942_AKYRO' IN (5201279007942) AS string_with_num
    , CAST( '5201279007942_AKYRO' AS UNSIGNED) IN (5201279007942) AS cast_to_int_with_num;

<强>样品

mysql> SELECT
    ->     '5201279007942_AKYRO' IN ('5201279007942') AS string_with_string
    ->     , '5201279007942_AKYRO' IN (5201279007942) AS string_with_num
    ->     , CAST( '5201279007942_AKYRO' AS UNSIGNED) IN (5201279007942) AS cast_to_int_with_num;
+--------------------+-----------------+----------------------+
| string_with_string | string_with_num | cast_to_int_with_num |
+--------------------+-----------------+----------------------+
|                  0 |               1 |                    1 |
+--------------------+-----------------+----------------------+
1 row in set, 2 warnings (0,00 sec)

mysql>