Home >Database >Mysql Tutorial >How to Check for Specific Numbers in a Comma-Separated List in MySQL?

How to Check for Specific Numbers in a Comma-Separated List in MySQL?

Susan Sarandon
Susan SarandonOriginal
2024-11-19 11:30:03194browse

How to Check for Specific Numbers in a Comma-Separated List in MySQL?

Checking Coma-Separated Numbers in MySQL

In a database with a table like the one provided, where NUMBERS is a comma-separated list, it's often necessary to check if specific numbers exist within that list. The LIKE %% syntax may not always be suitable for this purpose.

One effective solution is to utilize the IN operator. For example:

SELECT * FROM table WHERE 3 IN (NUMBERS) AND 15 IN (NUMBERS)

This query will select rows where both 3 and 15 are present in the NUMBERS column. The IN operator searches for a value within a comma-separated string, like the one in the NUMBERS column.

Therefore, in the provided example, only the row with ID 2 should be returned, as both 3 and 15 are found in its NUMBERS column.

The above is the detailed content of How to Check for Specific Numbers in a Comma-Separated 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