esolve
V2EX  ›  问与答

mysql 中如果列是 int 类型,但是 where 语句中是字符串,会有何问题?

  •  
  •   esolve · Jul 2, 2017 · 3064 views
    This topic created in 3264 days ago, the information mentioned may be changed or developed.

    比如一个表,列 id 的类型是 int 但是 sql 语句

    select * from table where id="10";

    运行没问题 但是这样的语句相比 select * from table where id=10;

    会有何种问题?

    5 replies    2017-07-03 12:55:48 +08:00
    yejinmo
        1
    yejinmo  
       Jul 2, 2017
    不负责猜测会有性能问题

    坐等楼下大佬详解
    ihuotui
        2
    ihuotui  
       Jul 2, 2017 via iPhone
    隐式转换自己看文档
    ihuotui
        3
    ihuotui  
       Jul 3, 2017 via iPhone   ❤️ 1
    @yejinmo 性能问题可以在高性能 mysql 一书提到,而且要考虑有没有索引,索引会失效,然后 mysql 要计算一次也是性能消耗
    ghos
        4
    ghos  
       Jul 3, 2017
    mysql 会把字符串转成 int 类型,有时候会导致索引失效
    2xShake
        5
    2xShake  
       Jul 3, 2017
    之前阿里云的 rds 提供的查询优化方案中, 有说如果数据库是字符串类型的请加 [“ ”] 双引号,不是则不需要咯。肯定跟性能有关系。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3600 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 10:35 · PVG 18:35 · LAX 03:35 · JFK 06:35
    ♥ Do have faith in what you're doing.