as the title states ,TINYINT (0, 1) for boolean values in MySQL return true and false value ,,but i want yes and no value when i retrieve TINYINT (0, 1) values from database. is it possible??
如标题所示,在MySQL中布尔值的TINYINT(0,1)返回true和false值,但是当我从数据库中检索TINYINT(0,1)值时,我想要是和没有值。可能吗??
5 个解决方案
#1
0
Wanting "yes" and "no" sounds like it's for display purposes, and how you choose to format data from your database/models for display should be in your view oriented code and not the database schema nor the query logic, though there are valid exceptions to the latter. Aside from considerations such as whether "first name" and "last name" as separate items could be useful rather than a single "name" field, decisions about database structure should be based on what you need to store and how different data items relate to each other, not on how data is going to look once retrieved. Particularly as you're using a framework, this should be straightforward.
希望“是”和“否”听起来像是用于显示目的,以及如何选择格式化数据库/模型中的数据以进行显示应该在面向视图的代码中,而不是数据库模式和查询逻辑,尽管有效后者的例外情况。除了诸如“名字”和“姓氏”作为单独项目是否有用而不是单个“名称”字段之类的考虑之外,关于数据库结构的决定应基于您需要存储的内容以及不同数据项与彼此,而不是一旦检索数据将如何看起来。特别是当您使用框架时,这应该是直截了当的。
That said, you could use an enum type instead, defining the enum as enum('no','yes'), though cake may still not have support for enums. You can then get and set string values or numeric ones (1 and 2, or subtract 1 to get 0 and 1). Overall though, tinyint with values 0 and 1 is probably preferable as it's conventional and less likely to lead to mistakes/bugs now or in the future.
也就是说,您可以使用枚举类型,将枚举定义为枚举('不','是'),尽管蛋糕可能仍然不支持枚举。然后,您可以获取并设置字符串值或数字值(1和2,或减1以获得0和1)。总的来说,值为0和1的tinyint可能更好,因为它是传统的,现在或将来不太可能导致错误/错误。
#2
5
Use IF:
SELECT IF(bool_value, 'yes', 'no') as string_value...
#3
2
tinyint doesnt return true or false. It returns 0 or 1. If you want yes and no, you need to specify that yourself:
tinyint不会返回true或false。它返回0或1.如果你想要是和否,你需要自己指定:
if($return == 0) {
return "no";
} else {
return "yes";
}
#4
1
Yes, it is possible. One way is to add CASE
to it:
对的,这是可能的。一种方法是向其添加CASE:
SELECT
CASE
WHEN value = true THEN 'yes'
ELSE 'no'
END
FROM
`table`;
Another one, as Max suggested, add IF
option.
正如Max建议的那样,另一个添加IF选项。
#5
0
Use AppModel::afterFind
http://book.cakephp.org/2.0/en/models/callback-methods.html#afterfind
public function afterFind($results, $primary = false) {
foreach ($results as $index => $row) {
if (isset($row[$this->alias]['yes_or_no'])) {
$results[$index][$this->alias]['yes_or_no'] = $row[$this->alias]['yes_or_no'] ? 'yes' : 'no';
}
}
return $results;
}
#1
0
Wanting "yes" and "no" sounds like it's for display purposes, and how you choose to format data from your database/models for display should be in your view oriented code and not the database schema nor the query logic, though there are valid exceptions to the latter. Aside from considerations such as whether "first name" and "last name" as separate items could be useful rather than a single "name" field, decisions about database structure should be based on what you need to store and how different data items relate to each other, not on how data is going to look once retrieved. Particularly as you're using a framework, this should be straightforward.
希望“是”和“否”听起来像是用于显示目的,以及如何选择格式化数据库/模型中的数据以进行显示应该在面向视图的代码中,而不是数据库模式和查询逻辑,尽管有效后者的例外情况。除了诸如“名字”和“姓氏”作为单独项目是否有用而不是单个“名称”字段之类的考虑之外,关于数据库结构的决定应基于您需要存储的内容以及不同数据项与彼此,而不是一旦检索数据将如何看起来。特别是当您使用框架时,这应该是直截了当的。
That said, you could use an enum type instead, defining the enum as enum('no','yes'), though cake may still not have support for enums. You can then get and set string values or numeric ones (1 and 2, or subtract 1 to get 0 and 1). Overall though, tinyint with values 0 and 1 is probably preferable as it's conventional and less likely to lead to mistakes/bugs now or in the future.
也就是说,您可以使用枚举类型,将枚举定义为枚举('不','是'),尽管蛋糕可能仍然不支持枚举。然后,您可以获取并设置字符串值或数字值(1和2,或减1以获得0和1)。总的来说,值为0和1的tinyint可能更好,因为它是传统的,现在或将来不太可能导致错误/错误。
#2
5
Use IF:
SELECT IF(bool_value, 'yes', 'no') as string_value...
#3
2
tinyint doesnt return true or false. It returns 0 or 1. If you want yes and no, you need to specify that yourself:
tinyint不会返回true或false。它返回0或1.如果你想要是和否,你需要自己指定:
if($return == 0) {
return "no";
} else {
return "yes";
}
#4
1
Yes, it is possible. One way is to add CASE
to it:
对的,这是可能的。一种方法是向其添加CASE:
SELECT
CASE
WHEN value = true THEN 'yes'
ELSE 'no'
END
FROM
`table`;
Another one, as Max suggested, add IF
option.
正如Max建议的那样,另一个添加IF选项。
#5
0
Use AppModel::afterFind
http://book.cakephp.org/2.0/en/models/callback-methods.html#afterfind
public function afterFind($results, $primary = false) {
foreach ($results as $index => $row) {
if (isset($row[$this->alias]['yes_or_no'])) {
$results[$index][$this->alias]['yes_or_no'] = $row[$this->alias]['yes_or_no'] ? 'yes' : 'no';
}
}
return $results;
}