MySQL uses bit arithmetic

Source: Internet
Author: User
Tags arithmetic

usually our data table may contain various state attributes, such as the blog table, we need to have a field to indicate whether it is public, whether there is a password set, whether the administrator blocked, whether it is pinned, and so on. Also encountered in the post OPS, planning requires adding new functionality and causing you to add new fields. This results in later maintenance difficulties, increased database size, and increased indexing. At this point, the use of bit arithmetic can be cleverly solved.
For example
--Public blog for status or Operation
UPDATE Blog SET status = Status | 1;
--Encrypt the blog for status or Operation
UPDATE Blog SET status = Status | 2;
--Block blog
UPDATE Blog SET status = Status | 4;
--Unlock blog
UPDATE Blog SET status = status ^ 4;
--Check all the blogs that are pinned
SELECT * from blog WHERE status & 8;

From for notes (Wiz)

MySQL uses bit arithmetic

Related Article

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.