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

2018-04-20 14:06:00 +08:00
 tttttttt

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

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

update test set email = 'tes@test.com' where name=0; update test set email = 'tes@test.com' where name=false;

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

1573 次点击
所在节点    问与答
5 条回复
tttttttt
2018-04-20 14:28:52 +08:00
有大佬知道为什么吗?
justfindu
2018-04-20 14:38:33 +08:00
看起来像是自动转类型并且计算了.
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
2018-04-20 16:17:20 +08:00
在插入前不强转的吗?
ihuotui
2018-04-20 22:49:06 +08:00
隐式转换

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/448450

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX