SQL operations for MYSQL SET Fields

Source: Internet
Author: User

Copy codeThe Code is as follows: <em> 'mark' set ('index', 'best', 'hot ') </em>

There were few users and few people who knew about it. After a long time, I finally got a set of knowledge points.
Looking at the above structure, MYSQL is not stupid. Will it store indexes? No, it stores numbers.
The SET field uses binary numbers to correspond to the values you SET one by one. For example, if the index is in The 0th bits, then the zero side of 2 is equal to 1.Copy codeThe Code is as follows: update from table SET mark = 1

Copy codeThe Code is as follows: update from table SET mark = 'index'

The above two results are the same.
Then the problem arises, just like zhimeng. If I want to add a popular article, but do not want to delete other identifiers, do it? Or I want to delete an identifier, I don't know what logo I used to have.

Add an ID
Copy codeThe Code is as follows: update from table SET mark = mark | 1 | 2

Don't ask why I use it | this stuff, which means adding the index and best identifiers. If you only add the hot identifiers, write them as | 4.

Below is the deletion identifier
Copy codeThe Code is as follows: update from table SET mark = mark &~ 4 &~ 1

Okay, I will talk about updates first, and I will talk about queries below.
Copy codeThe Code is as follows: SELECT * FROM table WHERE FIND_IN_SET ('hot ', mark)

This is the simplest query method. You can also use the binary number 4 in the hot location to replace the query. The efficiency is the same.
You can also write this statement:Copy codeThe Code is as follows: SELECT * FROM table WHERE mark & 1

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.