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

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

Linda Hamilton
Linda HamiltonOriginal
2024-11-07 17:16:03519browse

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)

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!

Statement:
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