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

pokxtpni  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(350)

数据库中有一列名为 active . 我有 bit(10) 数据类型。我将用于用户的访问。例如,该二进制数的第一个字符表示用户的投票能力。
无论如何,该列的定义如下:

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

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

UPDATE users SET active = 101 WHERE 1

不管怎样,为什么是31位数?为什么不能改变?我怎样才能得到一个10位的二进制数,它是可变的?

ljsrvy3e

ljsrvy3e1#

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

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 |

操场小提琴

5anewei6

5anewei62#

创建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

相关问题