Home > Article > Backend Development > Basic principles and examples of database field reuse_PHP tutorial
1. Basics of logical algebra:
1. Numbers are represented in binary, and the only possible numbers are 0 and 1.
2. There are only three basic operations: "and", "or" and "not".
The AND operation is defined as: (Use & to express the AND operation)
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
It can be simply understood as: as long as there is a 0, the result is 0, similar to multiplication.
The OR operation is defined as: (Use | to represent the AND operation)
0 | 0 = 0
0 | 1 = 1
1 | 0 = 1
1 | 1 = 1
It can be simply understood as: as long as there is a 1, the result is 1, similar to addition.
2. Examples of logical operations:
01111010101010101111111111111111 & 1100000 = 1100000
Generally can be understood as:
If you want to get the N-digit value of a number, you only need to AND the number with 2 raised to the N-1 power (mask).
3. Database field definition:
Take the data table binary_sample as an example:
create table binary_sample(
uid int unsigned not null,
status int unsigned not null default 0,
primary key(uid),
key i_s(status)
)engine=innodb;
Status field definition:
The data type of the status field is a 32-bit integer. In order to store as many attributes as possible, we define it as follows:
All the following “bits” are described in order from low to high (right to left).
0-2 digits indicate user registration status:
000 means the new registration has not been approved
001 means registration is approved
010 indicates an advanced user
011 means administrator
100 means super administrator
101 Reserved
110 Reserved
111 Mask
Gender of 3-5 users:
000 indicates gender uncertainty
001 indicates the gender is male
010 indicates the gender is female
011 Reserved
100 Reserved
101 Reserved
110 Reserved
111 Mask
If we want to query all male users:
Select * from binary_sample where status & b111000 = b001000;
If we want to query all administrator users:
select * from binary_sample where status & b111 = b011;
If we want to query all male administrator users:
Select * from binary_sample where status & b111111 = b001011;
If we want to query all non-newly registered and unapproved users then:
Select * from binary_sample where status & b111 != b000;
Fourth, use PHP program to perform such calculations:
define("USER_NEW",0);//000
define("USER_NORMAL",1);//001
define("USER_ADVANCE",2);//010
define("USER_MANAGE",3);//011
define("USER_SUPER",4);//100
define("USER_MASK",7);//111
define("GENDER_UNKNOWN",0);// 000000
define("GENDER_MALE",8);// 001000
define("GENDER_FEMALE",9);// 010000
define("GENDER_MASK",56);//111000
If we want to query all male users:
$status=GENDER_MALE;
$mask=GENDER_MASK;
$sql="select * from binary_sample where status & ${mask} = ${status}";
If we want to query all administrator users:
$status=USER_MANAGE;
$mask=USER_MASK;
$sql="select * from binary_sample where status & ${mask} = ${status}";
If we want to query all male administrator users:
$status=GENDER_MALE & USER_MANAGE;
$mask = GENDER_MASK & GENDER_MASK;
$sql="select * from binary_sample where status & ${mask} = ${status}";
If we want to query all non-newly registered and unapproved users then:
$status = USER_NEW;
$mask = USER_MASK;
$sql="select * from binary_sample where status & ${mask} != ${status}";
By analogy, as long as the meaning of each value is defined, the query is basically settled.