Home > Database > Mysql Tutorial > How to Search Within a Comma-Delimited List in MySQL?

How to Search Within a Comma-Delimited List in MySQL?

Linda Hamilton
Release: 2024-11-07 17:16:03
Original
596 people have browsed it

How to Search Within a Comma-Delimited List in MySQL?

Searching within a Comma-Delimited List in MySQL

Your MySQL table contains a field with a comma-separated list of IDs. You wish to search within this field using a query like SELECT ... WHERE field LIKE '%1%'. However, this query matches many entries due to the overlapping IDs.

To address this issue, consider the FIND_IN_SET function:

SELECT ... WHERE FIND_IN_SET('1', field)
Copy after login

The FIND_IN_SET function takes two arguments: the string you're searching for and the comma-separated list. It returns the position of the string within the list, or 0 if not found.

Using this function, your query can precisely locate IDs within the comma-separated list, regardless of their position. This will provide you with more accurate search results and avoid the issue of matching entries due to overlapping IDs.

The above is the detailed content of How to Search Within a Comma-Delimited List in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template