Learn the magic use of MySQL's Find_in_set function

Source: Internet
Author: User
Tags mysql manual

Later discovered Find_in_set this magical function, for example: There is a Type field in the article table, he stores the article type, has 1 headlines, 2 recommendations, 3 hotspots, 4 text ... 11,12,13, etc.

Now he has an article that is both a headline and a hotspot, or a text, stored in a 1,3,4 format in type.

So how do we use SQL to find all of the 4 text standards in the type, this is the time for our find_in_set.

First look at the syntax of the Find_in_set function in the MySQL manual:
Find_in_set (Str,strlist)

If string str is in the strlist of a string list of n chains, the return value ranges from 1 to N. A list of strings is a string of strings that are separated by a number of ', ' symbols. If the first argument is a constant string, and the second is the Type SET column, the Find_in_set () function is optimized, using bit computations. If STR is not strlist or strlist is an empty string, the return value is 0. If any of the arguments are null, the return value is null. This function will not work correctly when the first argument contains a comma (', ').

mysql> SELECT find_in_set (' B ', ' a,b,c,d ');
-> 2

It's easy to use, as I mentioned above, for example:

SELECT * from article WHERE find_in_set (' 4′,type)

Summary: It was thought that the Find_in_set function can only be applied to fields of the set type, and after testing, it is found that this function applies to all fields of non-numeric type, but the stored data must be comma-separated

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.