Example of "MySQL" MySQL find_in_set use

Source: Internet
Author: User

Excellent articles for > reference

Find_in_set (Str,strlist)

> A simple example

The function is to look for the first parameter in the second argument and return the position where the first argument is, or 0 if it does not exist. Where the second parameter is a string separated by ",".

--1
Select Find_in_set (' 1 ', ' 1,2,3,4,5,6 ');

--2SelectFind_in_set ('2','1,2,3,4,5,6');
--0SelectFind_in_set ('7','1,2,3,4,5,6');
--0SelectFind_in_set ('2','1,21,3,4,5,6');
--0SelectFind_in_set ('2',"');
--0SelectFind_in_set ('2','3,4,5,6.');
--NULLSelectFind_in_set (NULL,'1,2,3,4,5,6');
--NULLSelectFind_in_set (1,NULL);
--0SelectFind_in_set ('2,3','1,2,3,4,5,6');

> Partial use function for replaceable delimited strings

Sometimes we get the "2,3,4" string, and we want to separate it into 5 rows, each of which is 2, 3, 4, respectively.

At this point, we naturally think of a Java-like string split function, but MySQL does not seem to have similar functions. (I did not find, if any, please inform me OH)

In fact, "2,3,4" is a certain business meaning, or from a table, then you can use the Find_in_set "2,3,4" from the corresponding table to find out. In general, the key is indexed, from a large amount of data to find a small amount of data, the efficiency is very high.

The following is a simple example:

SelectT.id from (Select 1  asId'Nick'  asnameUnion  AllSelect 2  asId'Viki'  asnameUnion  AllSelect 3  asId'Robin'  asnameUnion  AllSelect 4  asId'Teng'  asnameUnion  AllSelect 5  asId'Mike'  asnameUnion  AllSelect 6  asId' would'  asname) TwhereFind_in_set (T.id,'2,3,4')> 0;
View Code

Example of "MySQL" MySQL find_in_set use

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.