您好我使用的休眠和MySQL。我有一个布尔属性的类叫做“活动的”。

生成的数据库表具有BIT数据类型。到现在为止还挺好。 我想查询此值,但我不知道该怎么做。 我已经试过

 SELECT * from table where active = 1

不工作,无论是以下

 SELECT * from table where active = true

我没有也不既不在参考手册在Stackoveflow发现任何东西。

任何暗示?

提前感谢!

有帮助吗?

解决方案

SELECT * FROM table WHERE active = (1)

其他提示

根据此页面,BIT为TINYINT对于之前版本的(1)的同义词5.0.3

您是否尝试过这些?

SELECT * from table where active = (1)
SELECT * from table where active = 'true'
SELECT * from table where active = b'1'

博客条目建议将避免BIT数据类型完全。

要指定位值,可以使用b'value”符号。

你试过它转换成一整数进行比较

SELECT * from table where cast(active as unsigned) = 1

我用MS SQL的大部分时间所以请原谅我,如果这也不行,因为我不能对它进行测试。

实际上MySQL已经内置位文字:

select*from table where active = 0b1

那么,对于两个比较和更新,0和1组的工作对我来说:

下面的类型位(1),一排的一字段,该字段被目前假:

mysql> select isfeatured from nodes where isfeatured = 1;
Empty set (0.00 sec)

mysql> select isfeatured from nodes where isfeatured = 0;
+------------+
| isfeatured |
+------------+
|            |
+------------+
1 row in set (0.00 sec)

更新在isfeatured改变0到1,它是类型位(1)...

mysql> update nodes set isfeatured=1 where isfeatured = 0;
Query OK, 1 row affected (0.05 sec)
Rows matched: 1  Changed: 1  Warnings: 0

一行改变......再试一次:

mysql> update nodes set isfeatured=1 where isfeatured = 0;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

没有行改变预期。

同一选择查询作为前:

mysql> select isfeatured from nodes where isfeatured = 1;
+------------+
| isfeatured |
+------------+
|           |
+------------+
1 row in set (0.00 sec)

mysql> select isfeatured from nodes where isfeatured = 0;
Empty set (0.01 sec)

请参阅,它的工作原理。

我使用:

的MySQL版14.14 DISTRIB 31年5月5日,对于使用的readline 6.2 Debian的Linux-GNU(x86_64的)

/ usr / sbin目录/ mysqld的版本5.5.31-0 + wheezy1用于在x86_64 Debian的Linux-GNU((Debian的))

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top