【问题标题】:Integer field with character in where clause returns strange outputwhere 子句中带有字符的整数字段返回奇怪的输出
【发布时间】:2014-11-03 18:06:10
【问题描述】:

我在表中有一个数据类型为 tiny int 的列。它的值像 1 和 0。我尝试了一个选择查询作为

SELECT * from table_name where filed_name = 'Y'; 

这会导致所有记录为零。我不明白查询执行是如何发生的。请帮助我理解这一点

【问题讨论】:

  • stackoverflow.com/questions/10852337/true-false-vs-0-1-in-mysql true 和 false 是 1 和 0 的同义词,请参阅上一个问题
  • 我需要从我的问题中了解,整数字段与字符比较如何返回 true(1)。如果您尝试上述查询,因为 'Select field_name = 'Y' from table_name' 将返回 1 作为结果。

标签: mysql


【解决方案1】:

string 转换为number 时,它变成了0 的值。 这就是为什么您会获得所有价值为 0

的记录
SELECT * from table_name where filed_name = 'Y';

这里'Y'自动转换为0

改为使用

SELECT * from table_name where filed_name = 1;

【讨论】:

  • 感谢 vikrant singh,现在我明白了。非常感谢
【解决方案2】:

TINYINT 不存储 'Y''N' 之类的字符串值,它存储整数 01。如果您想要所有值为“Y”的数据行,则必须按 1(整数)进行过滤,例如:

SELECT * FROM myTable WHERE myTinyColumn = 1

【讨论】:

    猜你喜欢
    • 2013-01-19
    • 2011-11-29
    • 2016-10-11
    • 1970-01-01
    • 2015-07-15
    • 1970-01-01
    • 2010-12-15
    • 2014-09-18
    • 2021-04-23
    相关资源
    最近更新 更多