How to use MySQL's FIND_IN_SET Function

Source: Internet
Author: User
Tags mysql manual
There is a type field in the Document Table, which stores the article type, including 1 headlines, 2 recommendations, 3 hotspots, and 4 texts ..... 11, 12, 13, etc.

There is a type field in the Document Table, which stores the article type, including 1 headlines, 2 recommendations, 3 hotspots, and 4 texts ..... 11, 12, 13, etc.

This is often the case when designing databases, for example:

There is a type field in the Document Table, which stores the article type, including 1 headlines, 2 recommendations, 3 hotspots, and 4 texts..., 12, and 13.

There is an article that is both toutiao, hotspot, and graphic,

Type is stored in the format of 1, 3, and 4.

How can we use SQL to search for articles with four text standards in all types,

This is the time for our find_in_set to crash.

First look at the syntax of the find_in_set function in the mysql manual:

FIND_IN_SET (str, strlist)

If the str string is in the strlist consisting of N substrings, the return value ranges from 1 to N. A string list is a string consisting of several tokens separated by commas. If the first parameter is a constant string and the second parameter is the type SET column, the FIND_IN_SET () function is optimized and computed in bits. If str is not in strlist or strlist is a null string, the return value is 0. If any parameter is NULL, the return value is NULL. This function cannot run normally when the first parameter contains a comma.

The Code is as follows:
Mysql> SELECT FIND_IN_SET ('B', 'a, B, c, D ');
-> 2

Easy to use

I will give an example of the above situation:

Reference content is as follows:
The Code is as follows:
Select * from article where FIND_IN_SET ('4', type)

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.