使用 SELECT 时看不到 MySQL BIT 字段值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14248554/
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
Can't see MySQL BIT field value when using SELECT
提问by Misha Moroshko
my_table
contains the enabled
field which is defined as: enabled BIT NOT NULL DEFAULT 0
.
my_table
包含enabled
定义为:的字段enabled BIT NOT NULL DEFAULT 0
。
This table has multiple rows with enabled = b'0'
, and multiple rows with enabled = b'1'
.
该表有多行带有enabled = b'0'
,多行带有enabled = b'1'
。
However, both this:
然而,这两个:
SELECT * from my_table WHERE enabled = b'0';
and this:
和这个:
SELECT * from my_table WHERE enabled = b'1';
show blank in the enabled
column:
在enabled
列中显示空白:
+----+---------+
| id | enabled |
+----+---------+
| 1 | |
| 2 | |
+----+---------+
Why is that? How could I see the value of the enabled
field?
这是为什么?我怎么能看到该enabled
字段的值?
$ mysql --version
mysql Ver 14.14 Distrib 5.1.63, for debian-linux-gnu (x86_64) using readline 6.1
回答by peterm
The reason why you can't see it in terminal is because bit values are non printable characters.
在终端中看不到它的原因是因为位值是不可打印的字符。
Lets insert following values:
让我们插入以下值:
INSERT INTO `my_table` (`ID`, `enabled`)
VALUES (1,b'1'),(2,b'0');
Then select them to file:
然后选择它们归档:
mysql> SELECT * FROM my_table INTO OUTFILE '/tmp/my_table.txt' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';
First lets view our /tmp/my_table.txt
file as plain text:
首先让我们/tmp/my_table.txt
以纯文本形式查看我们的文件:
"1"," "
"2"," "
“1”、“”
“2”、“”
and then in hex view:
然后在十六进制视图中:
22 31 22 2C 22 0122 0A 22 32 22 2C 22 0022 0A
22 31 22 2C 22 0122 0A 22 32 22 2C 22 0022 0A
To be able to see those values you can simply CAST
them in SELECT
:
为了能够看到这些值,您可以简单地将CAST
它们输入SELECT
:
SELECT id, CAST(enabled AS UNSIGNED) AS enabled FROM my_table
And that will produce the following output:
这将产生以下输出:
+----+---------+
| id | enabled |
+----+---------+
| 1 | 1 |
| 2 | 0 |
+----+---------+
2 rows in set (0.00 sec)
回答by L-Samuels
Another way you can do it is
另一种方法是
SELECT enabled+0 from my_table
回答by ??????
the simplest way is ORD
function:
最简单的方法是ORD
函数:
SELECT ORD(`enabled`) AS `enabled` FROM `my_table`
回答by tingshuai.yts
Bit values are returned as binary values. To display them in printable form, add 0 or use a conversion function such as BIN().
位值作为二进制值返回。要以可打印的形式显示它们,请添加 0 或使用 BIN() 等转换函数。
https://dev.mysql.com/doc/refman/5.7/en/bit-field-literals.html
https://dev.mysql.com/doc/refman/5.7/en/bit-field-literals.html
回答by Arjun Aravind
You could also try SELECT enabled&1 from my_table
.
你也可以试试SELECT enabled&1 from my_table
。