The correct answer would be to fix the table design by splitting the comma separated string into rows.
If the schema can not be changed , you could try the following approach which requires MySQL 8+.
We can use JSON_TABLE to split comma-separated values dynamically.
Consider the following data example
CREATE TABLE shirts (
id INT PRIMARY KEY,
colors VARCHAR(50) NOT NULL
);
INSERT INTO shirts VALUES (1,'1,2,3'), (2,'2,3,4'),
(3,'3,4'), (4,'1,2'), (5,'12,14');
Split the colors
SELECT s.id,
j.colors
FROM shirts s
JOIN JSON_TABLE(
CONCAT('[\"', REPLACE(REPLACE(s.colors,' ',''), ',', '\",\"'), '\"]'),
'$[*]' COLUMNS (colors VARCHAR(10) PATH '$')
) AS j
Retrieve all id that have colors "1" included
SELECT s.id,
j.colors
FROM shirts s
JOIN JSON_TABLE(
CONCAT('[\"', REPLACE(REPLACE(s.colors,' ',''), ',', '\",\"'), '\"]'),
'$[*]' COLUMNS (colors VARCHAR(10) PATH '$')
) AS j
WHERE j.colors = 1;
Above query we could use as a JOIN to retrieve all columns from the table
SELECT sh.*
FROM shirts sh
INNER JOIN (SELECT s.id,
j.colors
FROM shirts s
JOIN JSON_TABLE(
CONCAT('[\"', REPLACE(REPLACE(s.colors,' ',''), ',', '\",\"'), '\"]'),
'$[*]' COLUMNS (colors VARCHAR(10) PATH '$')
) AS j
WHERE j.colors = 1
) sub on sub.id=sh.id;
See example