【问题标题】:Don't want mysql to auto cast String to Integer不希望 mysql 自动将 String 转换为 Integer
【发布时间】:2019-10-14 09:21:20
【问题描述】:

我遇到了 MYSQL 从 String 到 Integer 的这种自动类型转换,这在我看来很奇怪。

mysql> select * from `isps` where `id` = '3ca6fb49-9749-3099-b30d-19ce56349ad6' OR `unique_id` = '3ca6fb49-9749-3099-b30d-19ce56349ad6';
+----+--------------------------------------+---------------+--------------+---------------------+---------------------+
| id | unique_id                            | name          | code         | created_at          | updated_at          |
+----+--------------------------------------+---------------+--------------+---------------------+---------------------+
|  3 | ee8db3be-1bf7-3440-8add-37232cfc4ecb | TTN           | ttn          | 2019-09-26 08:12:14 | 2019-09-26 08:12:14 |
|  7 | 3ca6fb49-9749-3099-b30d-19ce56349ad6 | ONE BROADBAND | onebroadband | 2019-09-26 08:12:14 | 2019-09-26 08:12:14 |
+----+--------------------------------------+---------------+--------------+---------------------+---------------------+

id = 3 我没有预料到结果,谁能帮忙解决这个问题。

我的数据库中的数据类型

id - BIGINT
unique_id - varchar(200)

【问题讨论】:

    标签: mysql type-conversion


    【解决方案1】:

    您可以在比较之前将id 转换为字符串。

    select * from `isps` where CAST(`id` AS CHAR) = '3ca6fb49-9749-3099-b30d-19ce56349ad6' OR `unique_id` = '3ca6fb49-9749-3099-b30d-19ce56349ad6';
    

    请注意,这会显着降低查询速度,因为它无法使用 id 列上的索引。

    【讨论】:

    • 嗨,我什至可以将一个整数传递给这个查询,然后它会读取为 id = '4' 或 unique_id = '4' 会起作用
    • 这将适用于此。如果你写WHERE CAST(id AS CHAR) = '4' 它会起作用。
    猜你喜欢
    • 1970-01-01
    • 2019-05-05
    • 2019-12-06
    • 2010-09-06
    • 2011-02-07
    • 2022-09-30
    • 1970-01-01
    相关资源
    最近更新 更多