mysql db 中的布尔字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4757406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Boolean Field in mysql db
提问by samir chauhan
I want to save my value as a boolean in my mysql database. But somehow Iam not able to save it , mysql automatically saves it of the type tinyInt.Also tell me the default values for boolean. how we pass the values?
我想在我的 mysql 数据库中将我的值保存为布尔值。但不知何故,我无法保存它,mysql 会自动将它保存为 tinyInt 类型。还要告诉我布尔值的默认值。我们如何传递值?
回答by Mchl
In MySQL BOOLEAN
type is a synonym for TINYINT
. There is no dedicated BOOLEAN
type.
The vaules accepeted, are those for TINYINT
i.e. 0 for false, 1-255 (preferably 1) for true.
在 MySQL 中,BOOLEAN
类型是TINYINT
. 没有专用BOOLEAN
类型。接受的值是TINYINT
ie 0 表示假,1-255(最好是 1)表示真。
回答by GordonM
MySQL doesn't really have a BOOLEAN type, if you create a BOOLEAN column it will actually be a TINYINT.
MySQL 并没有真正的 BOOLEAN 类型,如果您创建一个 BOOLEAN 列,它实际上将是一个 TINYINT。
Treating TINYINT as a boolean isn't too problematic though, if you treat 0 as false and non-0 as true then it's fine. In PHP a statement like if ($column)
will return true if $column is any value except 0 or something that evaluates to 0. If you need it to explicitly be a bool you can convert it easily enough by doing $column = ($column != 0);
不过,将 TINYINT 视为布尔值并没有太大问题,如果您将 0 视为假,将非 0 视为真,那就没问题了。在 PHP 中,if ($column)
如果 $column 是除 0 以外的任何值或计算结果为 0 的值,则类似语句将返回 true。$column = ($column != 0);