Fundamentals and examples of database field reuse

Source: Internet
Author: User

The basis of logical algebra:

1, numbers are represented by binary, and all possible numbers are only 0 and 12.

2, the basic operation only "and", "or", "not" three kinds.

and operations are defined as: (with & Representation and operation)

0 & 0 = 0

0 & 1 = 0

1 & 0 = 0

1 & 1 = 1

Can be simply understood as: as long as there is a 0, the result is 0, and multiplication similar.

Or operation is defined as: (with representations and operations)

0 0 = 0

0 1 = 1

1 0 = 1

1 1 = 1

Can be simply understood as: as long as there is a 1, the result is 1, and the addition is similar.

Second, the logical operation example:

01111010101010101111111111111111 & 1100000 = 1100000

It can generally be understood as:

If you want to get a numeric value of n bits of a number, you only need to do this with the 2 N-1 (mask).

Three, 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:

Status field An integer of 32bit of data type, to store as many attributes as possible, we define it as follows:

The order of descriptions for all of the following "bits" is expressed in order from low to high (right to left).

0-2-bit indicates user registration status:

000 indicates that the new registration has not been approved

001 means registration is approved

010 represents a bit of advanced user

011 means admin

100 means Super Admin

101 Retention

110 retention

111 Masks

3-5-digit user sex:

000 indicates gender uncertainty

001 indicates gender as male

010 indicates gender as female

011 Retention

100 Retention

101 Retention

110 retention

111 Masks

If we want to query all male users:

SELECT * from binary_sample where status & B ' 111000 ' = B ' 001000 ';

If we want to query all admin users:

SELECT * from binary_sample where status & B ' n ' = B ' 011 ';

If we want to query all the male admin users:

SELECT * from binary_sample where status & B ' 111111 ' = B ' 001011 ';

If we want to query all non-new registrations for unapproved users:

SELECT * from binary_sample where status & B '!= ' B ' 000 ';

Use a PHP program to do this kind of calculation:

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 admin users:

$status =user_manage;

$mask =user_mask;

$sql = "SELECT * from Binary_sample where status & ${mask} ' = ${status}";

If we want to query all the male admin 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-new registrations for unapproved users:

$status = user_new;

$mask = User_mask;

$sql = "SELECT * from Binary_sample where status & ${mask}!= ${status}";

So, as long as you define the meaning of each value, the query is basically set.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.