How to use Find_in_set in MySQL

Source: Internet
Author: User

In MySQL, sometimes when we are doing database queries, we need to get a record of a certain value in a field, but it is not solved with like, we can find the record that we do not want, and it is more accurate than the find_in_. The set function comes in handy, and here's a concrete look.

Find_in_set (str,strlist) function

STR string to query for

Strlist field name parameters are separated by "," as (1,2,6,8)

Query field (strlist) contains the result of (str), the result is null or the record

The following examples illustrate

The test table has the following fields and values

Below I want to query the area contains "1″ This parameter record

SELECT * FROM Test where find_in_set (' 1 ', area)

return value

The following query btype field contains "15″ the value of this parameter

SELECT * FROM Test where find_in_set (' btype ')

return value

The following query btype field contains "5″ the value of this parameter

SELECT * FROM Test where Find_in_set (' 5 ', btype)

The return value is NULL because there is no value of "5" in Btype, which is different from a like fuzzy query, which separates values with ","

Next Query the Btype field contains the value of the 20″ parameter

SELECT * FROM Test where find_in_set (' a ', btype)

Of course, its return value is NULL, because there is no value in the field

The difference between find_in_set and like

Like is a broad fuzzy match, there is no delimiter in the string, Find_in_set is exact match, the field value is delimited in English "," and the result of the Find_in_set query is less than the result of the as query.

Reference URL: http://www.cnblogs.com/manongxiaobing/p/4682698.html

How to use Find_in_set in MySQL

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.