Sharing how to use MySQL's FIND_IN_SET function_PHP tutorial

WBOY
Release: 2016-07-21 15:19:52
Original
740 people have browsed it

Many times we have this situation when designing a database. For example:

There is a type field in the article table. It stores the article type. There are 1 headline, 2 recommendation, 3 hot spot, and 4 picture and text. .....11, 12, 13, etc.

Now there is an article that is both a headline, a hot topic, and a picture and text, in the format of 1, 3, 4 in

type Storage.

How do we use sql to find all articles with 4 image and text standards in type?

This is the time for our find_in_set to take action.

First look at the syntax of the find_in_set function in the mysql manual:

FIND_IN_SET(str, strlist)

If the string str is in the string list strlist composed of N sub-chains, the range of the return value Between 1 and N. A string list is a chain of strings separated by ',' characters. If the first argument is a constant string and the second is a column of type SET, the FIND_IN_SET() function is optimized to use bit calculations. If str is not in strlist or strlist is an empty string, the return value is 0. If any parameter is NULL, the return value is NULL. This function will not work properly if the first argument contains a comma (',').

Copy code The code is as follows:

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

It is very simple to use

Take the situation I mentioned above as an example:

The following is Quoted content:
Copy code The code is as follows:

select * from article where FIND_IN_SET('4',type)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325196.htmlTechArticleMany times we have this situation when designing a database, for example: there is a type field in the article table, It stores article types, including 1 headline, 2 recommendations, 3 hot spots, 4 pictures and texts...11, 12, 13 and so on...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!