Basic principles and examples of database field reuse _php Tutorial

Source: Internet
Author: User
First, the basis of logical algebra:

1, the number is represented by binary, and all possible occurrences are only 0 and 12.

2, the basic operation is only "with", "or", "non" three kinds.

With operations defined as: (with & Representations and operations)

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 the multiplication is similar.

Or operation defined as: (with | Representation and operation)

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.

Examples of logical operations:

01111010101010101111111111111111 & 1100000 = 1100000

It can generally be understood as:

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

Third, the database field definition:

Take data Sheet 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;

The Status field defines:

The Status field data type is an integer of 32bit, in order to store as many properties 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).

The 0-2-bit indicates the user registration status:

000 indicates that the new registration has not been approved

001 means registration is approved

010 indicates a bit advanced user

011 means administrator

100 means Super Admin

101 Reserved

110 reserved

111 Mask

3-5 Users Sex:

000 indicates gender uncertainty

001 indicates gender is male

010 indicates gender is female

011 Reserved

100 reserved

101 Reserved

110 reserved

111 Mask

If we want to query all the male users:

SELECT * from Binary_sample where status & b111000 = b001000;

If we want to query all the Admin users:

SELECT * from Binary_sample where status & b111 = b011;

If we want to query all male admin users:

SELECT * from Binary_sample where status & b111111 = b001011;

If we want to query all non-new registrations that are not approved by the user:

SELECT * from Binary_sample where status & b111! = b000;

Four, use the 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 the male users:

$status =gender_male;

$mask =gender_mask;

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

If we want to query all the Admin users:

$status =user_manage;

$mask =user_mask;

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

If we want to query all 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 that are not approved by the user:

$status = user_new;

$mask = User_mask;

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

And so on, as long as the meaning of each value is defined, the query is basically fixed.

http://www.bkjia.com/PHPjc/478751.html www.bkjia.com true http://www.bkjia.com/PHPjc/478751.html techarticle first, the logical algebra basis: 1, the number is represented by the binary, all possible occurrences of the number only 0 and 12. 2, the basic operation is only "with", "or", "non" three kinds. With the operation definition ...

  • 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.