在mysql中对bool或boolean数据类型这样描述:
BOOL, BOOLEAN
These types are synonyms for TINYINT(1). A value of zero is considered false. Nonzero values are considered true:
mysql> SELECT IF(0, 'true', 'false');
+------------------------+
| IF(0, 'true', 'false') |
+------------------------+
| false |
+------------------------+
mysql> SELECT IF(1, 'true', 'false');
+------------------------+
| IF(1, 'true', 'false') |
+------------------------+
| true |
+------------------------+
mysql> SELECT IF(2, 'true', 'false');
+------------------------+
| IF(2, 'true', 'false') |
+------------------------+
| true |
+------------------------+
However, the values TRUE and FALSE are merely aliases for 1 and 0, respectively, as shown here:
mysql> SELECT IF(0 = FALSE, 'true', 'false');
+--------------------------------+
| IF(0 = FALSE, 'true', 'false') |
+--------------------------------+
| true |
+--------------------------------+
mysql> SELECT IF(1 = TRUE, 'true', 'false');
+-------------------------------+
| IF(1 = TRUE, 'true', 'false') |
+-------------------------------+
| true |
+-------------------------------+
mysql> SELECT IF(2 = TRUE, 'true', 'false');
+-------------------------------+
| IF(2 = TRUE, 'true', 'false') |
+-------------------------------+
| false |
+-------------------------------+
mysql> SELECT IF(2 = FALSE, 'true', 'false');
+--------------------------------+
| IF(2 = FALSE, 'true', 'false') |
+--------------------------------+
| false |
+--------------------------------+
对bool或boolean的另外一方面的说明
boolean类型
MYSQL保存BOOLEAN值时用1代表TRUE,0代表FALSE,boolean在MySQL里的类型为tinyint(1),
MySQL里有四个常量:true,false,TRUE,FALSE,它们分别代表1,0,1,0,
mysql> select true,false,TRUE,FALSE;
+------+-------+------+-------+
| TRUE | FALSE | TRUE | FALSE |
+------+-------+------+-------+
| 1 | 0 | 1 | 0 |
+------+-------+------+-------+
可以如下插入boolean值:insert into [xxxx(xx)] values(true),当然也可以values(1);
举例如下:
mysql> alter table test add isOk boolean;
Query OK
mysql> desc test;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| isOk | tinyint(1) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
mysql> insert into test(isOk) values(true);
Query OK
mysql> select isOk from test ;
+------+
| isOk |
+------+
| 1 |
+------+
=================
MySQL没有boolean类型。这也是比较奇怪的现象。例:
create table xs
(
id int primary key,
bl boolean
)
这样是可以创建成功,但查看一下建表后的语句,就会发现,mysql把它替换成tinyint(1)。也就是说mysql把boolean=tinyInt了,但POJO类要定义成什么类型呢?
因为惯性思维,在java类中也把它定义成type。然后在Struts中使用<s:check/>标签。这就产生一个严重的问题了。<s:check>是boolean,而POJO去定义成byte。这样数据永远也无法提交,被struts的intercept拦截掉了。解决办法是在POJO类中定义成boolean,在mysql中定义成tinyint(1)。
------
TINYINT(1) or ENUM( 'true' , 'false')
-------
mysql中if函数的使用
MYSQL 手册中是这么解释的:
IF(expr1,expr2,expr3)
如果 expr1 是TRUE (expr1 <> 0 and expr1 <> NULL),则 IF()的返回值为expr2; 否则返回值则为 expr3。IF() 的返回值为数字值或字符串值,具体情况视其所在语境而定。