如何更新位(10)数据类型列?

问题描述 投票:0回答:2

我在数据库中有一个名为active的列。我有bit(10)数据类型。我将用于用户访问。例如,该二进制数的第一个字符指向用户投票的能力。

无论如何,该列的定义如下:

enter image description here

但令人惊讶的是,它看起来是一个31位二进制数字,所有数字都是1,并且根本不会更改为0。 (预期结果是10位二进制数)

enter image description here

注意:此查询不会对结果进行任何更改:

UPDATE users SET active = 101 WHERE 1

无论如何,为什么31位?为什么那不可改变?我怎样才能得到一个可更改的10位二进制数?

mysql sql
2个回答
0
投票

您应该使用二进制表示法来更新或插入您的值:

 UPDATE users SET active = b'101' WHERE 1

我已经测试过,我无法重现你的位(10)成为位(31)的问题。

我猜phpmyadmin没有正确显示你错误插入的值

我建议您将值输出为BIN(active)CAST(active AS UNSIGNED),以查看存储的实际二进制值是多少

CREATE table test (id INT, tip VARCHAR(100), active bit(10) NULL DEFAULT NULL );

INSERT into test (id,tip) VALUES (1,'testing NULL default');
INSERT into test  VALUES (2, 'testing 0101 without "b" notation', 0101);
INSERT into test  VALUES (3, 'testing 0101 with b notation ', b'0101');
INSERT into test  VALUES (4, 'testing 111 ', 111);
INSERT into test  VALUES (5, 'testing b1111111111 ', b'1111111111');


SELECT id, tip, BIN(active) as binvalue, CAST(active AS UNSIGNED) intvalue  FROM test;

| id |                               tip |   binvalue | intvalue |
|----|-----------------------------------|------------|----------|
|  1 |              testing NULL default |     (null) |   (null) |
|  2 | testing 0101 without "b" notation |    1100101 |      101 |
|  3 |     testing 0101 with b notation  |        101 |        5 |
|  4 |                      testing 111  |    1101111 |      111 |
|  5 |              testing b1111111111  | 1111111111 |     1023 |

Playground Fiddle


0
投票

创建11列并存储数据。选择第一列作为主键并将其设置为自动增量。

CREATE TABLE `test` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `column` bit(1) NOT NULL,
 `column` bit(1) NOT NULL,
 `column` bit(1) NOT NULL,
 `column` bit(1) NOT NULL,
 `column` bit(1) NOT NULL,
 `column` bit(1) NOT NULL,
 `column` bit(1) NOT NULL,
 `column` bit(1) NOT NULL,
 `column` bit(1) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1
© www.soinside.com 2019 - 2024. All rights reserved.