MariaDB FIND_IN_SET(): Find a Token in a Comma-Separated List
MariaDB’s FIND_IN_SET(value, comma_list) returns the position of the first matching item, starting at 1. It returns 0 when the item is absent and NULL when either argument is NULL.
Check the item position
The second argument to FIND_IN_SET() is one string with commas between its items. Run both statements in a MariaDB client. No table is required.
SELECT FIND_IN_SET('B', 'AA,B,CC,D') AS item_position,
FIND_IN_SET('B', 'A,B,B,D') AS first_match,
FIND_IN_SET('Z', 'A,B,C,D') AS not_found,
FIND_IN_SET('B', '') AS empty_list,
FIND_IN_SET(NULL, 'A,B') AS missing_value,
FIND_IN_SET('B', NULL) AS missing_list;
SELECT LOCATE('B', 'AA,B,CC,D') AS character_position;
The first row returns 2, 2, 0, 0, NULL, NULL. In AA,B,CC,D, B is item 2. The second expression also returns 2 because it stops at the first matching B. In the next statement, LOCATE() returns 4, the character position where B begins in the same string.
Spaces, letter case, and commas
A space after a comma can be part of the item. The queries below compare leading spaces, partial matches, a search value containing a comma, and two collation rules.
SELECT FIND_IN_SET('B', 'A, B,C') AS without_space,
FIND_IN_SET(' B', 'A, B,C') AS with_space,
FIND_IN_SET('B', 'AB,BC') AS partial_match,
FIND_IN_SET('a,b', 'a,b,c') AS comma_in_value;
SELECT FIND_IN_SET(_utf8mb4'b' COLLATE utf8mb4_general_ci, 'A,B,C') AS ignore_case,
FIND_IN_SET(_utf8mb4'b' COLLATE utf8mb4_bin, 'A,B,C') AS distinguish_case;
The first row returns 0, 2, 0, 0. B differs from B and from AB. A value containing a comma cannot be searched reliably with this function. Use a CSV parser for data that uses quotes to escape commas within a field. The second row gives 2 with case-insensitive comparison and 0 with the binary collation used here. If your input rules ignore outside spaces, trim each item as it enters the list so spaces inside names are preserved.
Filter a legacy tag list
Suppose a legacy VARCHAR column stores tags such as sql,php. The common table expression (CTE) supplies sample rows and shows the position alongside the true or false result of each membership test.
WITH sample_tags AS (
SELECT 1 AS id, 'php,sql' AS tags
UNION ALL SELECT 2, 'php,css'
UNION ALL SELECT 3, 'nosql'
UNION ALL SELECT 4, ''
UNION ALL SELECT 5, NULL
), tag_positions AS (
SELECT id, tags, FIND_IN_SET('sql', tags) AS sql_position
FROM sample_tags
)
SELECT id, tags, sql_position,
sql_position > 0 AS contains_sql,
sql_position = 0 AS known_without_sql
FROM tag_positions
ORDER BY id;
In row 1, sql is the second item and only the inclusion test returns 1. Rows 2, 3, and 4 produce position 0, including nosql in row 3. Both tests return NULL for row 5. In a real table use WHERE FIND_IN_SET('sql', tags) > 0 for matching rows and WHERE FIND_IN_SET('sql', tags) = 0 for known lists without the tag. Add OR tags IS NULL to the latter only if unknown lists also count as absent.
A SET column versus a text list
The SET type stores selections from a list declared in the schema. For this example, select a practice database where you have CREATE TEMPORARY TABLES permission. The sample table exists only in the current connection and the script drops it at the end.
CREATE TEMPORARY TABLE find_in_set_demo (
id INT PRIMARY KEY,
colors SET('red', 'green', 'blue') NULL
);
INSERT INTO find_in_set_demo VALUES (1, 'blue,red'), (2, NULL);
SELECT id, colors,
FIND_IN_SET('blue', colors) AS set_position,
FIND_IN_SET('green', colors) AS absent_position,
FIND_IN_SET('blue', CAST(colors AS CHAR)) AS text_position
FROM find_in_set_demo
ORDER BY id;
DROP TEMPORARY TABLE find_in_set_demo;
The first row displays red,blue in declaration order. set_position is 3 because blue is third in the declared set. After conversion to CHAR, its position in the displayed list is 2. Unselected green returns 0, and the second row returns NULL for all positions. MariaDB can optimize the lookup with bit arithmetic when the second argument has type SET.
For frequently changed tags or repeated tag lookups, use one row per relationship in a table such as article_tags(article_id, tag_id). A composite primary key prevents duplicate relationships, and an index on (tag_id, article_id) supports lookups by tag. Check the execution plan on your data if you need to keep searching a text list.




