Query values in comma separated string in MySQL
P粉763748806
2023-08-17 14:24:54
<p>I have a field <code>COLORS (varchar(50))</code> in my table <code>SHIRTS</code> which contains a comma separated string like < ;code>1,2,5,12,15,</code>. Each number represents an available color. </p>
<p>When running the query <code>select * from shirts where colors like '%1%'</code> to get all shirts that are red (color=1), I also get the color is gray (= 12) and orange (=15) shirts. </p>
<p>How should I rewrite the query so that it only selects all colors with the color 1 instead of all colors containing the number 1? </p>
FIND_IN_SET is your friend in this case
The classic method is to add commas on the left and right sides:
But find_in_set can also be used: