V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
tttttttt
V2EX  ›  问与答

mysql 数据更新导致全部数据更新相关

  •  
  •   tttttttt · 2018-04-20 14:06:00 +08:00 · 1541 次点击
    这是一个创建于 2169 天前的主题,其中的信息可能已经有所发展或是发生改变。

    有一张表 test,大致字段为下

    id int(10)
    name varchar(255)
    email varchar(255)
    

    update test set email = '[email protected]' where name=0; update test set email = '[email protected]' where name=false;

    我们的 name 字段在数据库为字符串,但当我们 sql 中让 name 字段等于一个 int 0 或 boole false 时, 都会导致把所有数据都更新,请教下是因为什么?

    5 条回复    2018-04-20 22:49:06 +08:00
    tttttttt
        1
    tttttttt  
    OP
       2018-04-20 14:28:52 +08:00
    有大佬知道为什么吗?
    justfindu
        2
    justfindu  
       2018-04-20 14:38:33 +08:00
    看起来像是自动转类型并且计算了.
    DT27
        3
    DT27  
       2018-04-20 15:31:44 +08:00
    数据库字段定义的是什么类型就是什么类型,
    "当我们 sql 中让 name 字段等于一个 int 0 或 boole false 时",存进去的也是 varchar 类型。
    你用"where name=0"或者"where name=false"时,类型跟字段不符,mysql 自动把字段里的值转为 0 或 false 的类型。
    任何字符串在 mysql 中转换为 int 后都等于 0 即 false。
    night98
        4
    night98  
       2018-04-20 16:17:20 +08:00 via Android
    在插入前不强转的吗?
    ihuotui
        5
    ihuotui  
       2018-04-20 22:49:06 +08:00 via iPhone
    隐式转换
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1063 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 19:05 · PVG 03:05 · LAX 12:05 · JFK 15:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.