我有一张这样的表:
// numbers
+---------+------------+
| id | numb |
+---------+------------+
| int(11) | bit(10) |
+---------+------------+
| 1 | 0000000001 |
| 2 | 0000000101 |
| 3 | 0000000000 |
| 4 | 0000001011 |
+---------+------------+
当我获取numb
列时,结果将如下:
// Query
mysql> SELECT numb FROM numbers WHERE id = 2
// Fetching by PHP (pdo)
$result = $stm->fetch();
$numb = $result['numb'];
echo $numb;
//=> 5
如您所见,最终结果为5
。虽然我想将这个确切的值作为字符串,例如0000000101
。我怎么能这样做?
答案 0 :(得分:3)
您可以使用decbin()功能和sprintf用于预先挂起的零
0000000101
输出:
12 - 0000001100
26 - 0000011010
来自您的评论:
$numb
<强> UPDATE1:强>
您可以像这样分配$numb = sprintf("%010d",decbin($result['numb']));
变量:
CREATE TABLE person
( id NUMBER(9,0) NOT NULL
, name VARCHAR2(30) NOT NULL
, gender VARCHAR2(1) NOT NULL
, fatherid NUMBER(9,0) NULL
, motherid NUMBER(9,0) NULL
, father_gender VARCHAR2(1) GENERATED ALWAYS AS ('M') VIRTUAL
, mother_gender VARCHAR2(1) GENERATED ALWAYS AS ('F') VIRTUAL
, CONSTRAINT ck_person_gender CHECK (gender IN ('M', 'F'))
, CONSTRAINT pk_person PRIMARY KEY (id)
, CONSTRAINT uk_person UNIQUE (id, gender)
, CONSTRAINT fk_person_father FOREIGN KEY (fatherid, father_gender)
REFERENCES person (id, gender) ON DELETE CASCADE
, CONSTRAINT fk_person_mother FOREIGN KEY (motherid, mother_gender)
REFERENCES person (id, gender) ON DELETE CASCADE
) TABLESPACE users;
答案 1 :(得分:3)