Search GROUP_CONCAT column
23 April 2025 (Updated 18 May 2025)
Suppose you produce a column using GROUP_CONCAT() that contains a bunch of IDs and you want to search that column to find rows where the column contains a particular ID. You can do so using FIND_IN_SET()
:
SELECT
user_id,
GROUP_CONCAT(project_id ORDER BY project_id) AS project_ids
FROM
user_projects
GROUP BY
user_id
HAVING
FIND_IN_SET('42', project_ids) > 0;
The above query will filter the user_projects
rows so that we fetch only the rows where the project_ids
column which will contain something like 101, 202, 303
contains the value 42
.
Tagged:
SQL recipes