gpt4 book ai didi

php - Where 子句在传递未知字符串值后检索数据。为什么?

转载 作者:太空宇宙 更新时间:2023-11-03 11:32:05 25 4
gpt4 key购买 nike

SELECT * FROM `table_name` WHERE `column_name` = "60dsfdsf"

在 phpmyadmin 中,如果运行这个查询,那么它将从 table_name 中检索 column_name 值 60。

这里 column_name 类型是 int 和 primary。

我原以为找不到任何值,但它正在检索 column_name 值 60

这背后的原因是什么?

最佳答案

好吧,这是根据其类型转换值的 MySQL。如果该列的类型为 INT 并且传递的值包含具有数字的字符串,它将从中提取数字。

根据 type-conversion-MySql

When an operator is used with operands of different types, type conversion occurs to make the operands compatible. Some conversions occur implicitly. For example, MySQL automatically converts numbers to strings as necessary, and vice versa.

所以在你的情况下,

SELECT * FROMtable_nameWHEREcolumn_name= "60dsfdsf"

实际上是转换为

SELECT * FROM表名WHEREcolumn_name= "60"

编辑:

如果您的字符串以数字开头,则 MySql 将采用该数字。

例如。 60dsfdsf60

但是,如果字符串最初有字母那么结果将是

例如。 dsfdsf60 对于 MySQL 是 0

所以要小心,因为每种情况的结果都不相同。更好的方法是在传递值之前转换或提取数字,然后将其发送给查询。

希望对你有帮助

关于php - Where 子句在传递未知字符串值后检索数据。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48981474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com