MySQL query finding values in a comma separated string

后端 未结 11 1388
孤独总比滥情好
孤独总比滥情好 2020-11-21 23:40

I have a field COLORS (varchar(50)) in a my table SHIRTS that contains a comma delimited string such as 1,2,5,12,15,. Each number repr

11条回答
  •  悲哀的现实
    2020-11-22 00:03

    You should actually fix your database schema so that you have three tables:

    shirt: shirt_id, shirt_name
    color: color_id, color_name
    shirtcolor: shirt_id, color_id
    

    Then if you want to find all of the shirts that are red, you'd do a query like:

    SELECT *
    FROM shirt, color
    WHERE color.color_name = 'red'
      AND shirt.shirt_id = shirtcolor.shirt_id
      AND color.color_id = shirtcolor.color_id
    

提交回复
热议问题