(Mysql) how to determine whether a numeric variable is included in a field of the table and list all data in the table containing the variable. how can this problem be solved?

Source: Internet
Author: User
(Mysql) how to determine whether a numeric variable is included in a table field and list all data in the table containing the variable. if the table contains the following idcsite fields .... 11,2, 13 .... & nbsp; 22,3, 4 (mysql) how to determine whether a numeric variable is included in a table field and list all data in the table containing the variable.
If the table contains the following fields:
Id csite ....
1 1, 2, 13 ....
2, 3, 4 ....
3, 5 ....
4, 5, 23 ....
5 1, 3, 8 ....

Suppose we get a number variable 3 and how to list all data rows containing the number 3

Use select id, csite from table where csite like '% 100' or csite like' % 3, % 'or csite like' %, 3, % 'or csite like' %, 3%'

This will not work, and we will also find out 13, 23

The expected result is:
Id csite ....
2, 3, 4 ....
5 1, 3, 8 ....

I checked some information and tried using this charindex (3, csite)> 0. what should I do ??

Not too complex SQL statements and stored procedures

Thank you !!!!!



------ Solution --------------------
You can use the MySQL function FIND_IN_SET.

FIND_IN_SET (str, strlist)
Returns a value if the str string is in the strlist consisting of N substrings, a value ranging from 1 to N is returned. A string list consists of multiple substrings separated by the character. If the first parameter is a constant string and the second parameter is of the SET column type, the FIND_IN_SET () function will be optimized to use bitwise operations! If str is not in strlist or if strlist is an empty string, the return value is 0. If any parameter is NULL, the return value is also NULL. If the first parameter contains a ",", this function will not work at all:
Mysql> SELECT FIND_IN_SET ('B', 'a, B, c, D ');
-> 2

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.