MYSQL set field type how to query

Source: Internet
Author: User

A set can contain up to 64 members whose value is an integer. (Set type base check MySQL data type set type) The binary of this integer indicates which members of the set's value are true. For example, there is a ' Status ' set

The code is as follows Copy Code
(' Forsale ', ' authsuccess ', ' auditsuccess ', ' intentionreached ', ' salecanceled '), their values are:
SET member Decimal Value Binary value
-----------------------------
Forsale 1 0001
AuthSuccess 2 0010
Auditsuccess 4 0100
Intentionreached 8 1000

If you deposit 9 in the Status field, the binary value is 1001, which means ' forsale ' and ' intentionreached ' are true for this value.
It can be thought that if so, you can use the like command and the Find_in_set () function to retrieve the SET value:

The code is as follows Copy Code
SELECT * from Tbl_name WHERE Status is like '%value% ';

SELECT * from Tbl_name WHERE find_in_set (' value ', Status) >0;

Of course, the following SQL statements are also legal and they appear to be more concise (note the order of two members and connectors):

The code is as follows Copy Code
SELECT * from tbl_name WHERE Status = ' forsale,intentionreached '; at the same time we can also use the value directly to query:
SELECT * from tbl_name WHERE Status = 9

Because the position of the members of the set type is fixed, the value 9 always represents ' Forsale ' and ' intentionreached ' as true, so that even if you add a state later, it will not affect the previous query logic. Let's take a look at the use of SQL statements to modify the Set Type field:
Modify status so that its ' forsale ' members are true

The code is as follows Copy Code
UPDATE tbl_name SET Status = 1 WHERE Id = 333

Modify status so that its ' forsale ', ' authsuccess ', ' intentionreached ' members are true

The code is as follows Copy Code
UPDATE tbl_name SET Status = 1 | 2 | 4 WHERE Id = 333;

In the confirmation of the current status of 7 o'clock Remove ' forsale ' members, let 7 also as the operation of the condition is to ensure that the current state is 7, if not this requirement can be directly SET

The code is as follows Copy Code
Status = 6;
UPDATE tbl_name SET Status = 7 & ~ WHERE Id = 333;

Oh, the use of set types to save permissions and other data really makes the development of a lot of convenient and clear wow.

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.