How to use the Find_in_set function of MySQL

Source: Internet
Author: User
Tags mysql manual

For example:
There is a Type field in the article table, he stores the article type, there are 1 headlines, 2 recommended, 3 hot, 4 graphics ..... 11,12,13, wait.

Now there is an article he is a headline, but also hot, or graphic,

The type is stored in 1,3,4 format.

How do we use SQL to find all of the 4 text standards in the type??


It's time for our find_in_set to come.

The following is the referenced content:

SELECT * from article where Find_in_set (' 4 ', type)

----------------------------------------------------------
The syntax for the Find_in_set function in the MySQL manual:

Find_in_set (Str,strlist)

If the string str is in a string list strlist consisting of N substrings, the return value ranges from 1 to N.
A string list is a string of sub-chains that are separated by ', ' symbols. If the first argument is a constant string and the second is a type set column, the Find_in_set () function is optimized to use bit calculations.
If STR is not strlist or strlist is an empty string, the return value is 0. If either parameter is NULL, the return value is null. This function will not work correctly when the first parameter contains a comma (', ').

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

2 because B is placed at 2 in the Strlist collection starting from 1
--------------------------------------------------------
Select Find_in_set (' 1 ', ' 1 '); The return is 1. The Strlist collection is a bit special. Only one string actually requires the previous string to return a number greater than 0 in the next string collection.

Select Find_in_set (' 2 ', ' + '); return 2
Select Find_in_set (' 6 ', ' 1 '); Returns 0
----------------------------------------------------------

Attention:
SELECT * from TreeNodes where Find_in_set (ID, ' 1,2,3,4,5 ');
Return multiple records at once using the Find_in_set function

ID is a field of a table and then each record is an ID equal to 1,2,3,4,5 when
Sort of like in (collection)
SELECT * from TreeNodes where ID in (1,2,3,4,5);

(go) MySQL Find_in_set function use method

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.