MySQL query finding values in a comma separated string

后端 未结 11 1422
孤独总比滥情好
孤独总比滥情好 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:11

    This will work for sure, and I actually tried it out:

    lwdba@localhost (DB test) :: DROP TABLE IF EXISTS shirts;
    Query OK, 0 rows affected (0.08 sec)
    
    lwdba@localhost (DB test) :: CREATE TABLE shirts
        -> (
    -> id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, -> ticketnumber INT, -> colors VARCHAR(30) -> );
    Query OK, 0 rows affected (0.19 sec) lwdba@localhost (DB test) :: INSERT INTO shirts (ticketnumber,colors) VALUES -> (32423,'1,2,5,12,15'), -> (32424,'1,5,12,15,30'), -> (32425,'2,5,11,15,28'), -> (32426,'1,2,7,12,15'), -> (32427,'2,4,8,12,15'); Query OK, 5 rows affected (0.06 sec) Records: 5 Duplicates: 0 Warnings: 0 lwdba@localhost (DB test) :: SELECT * FROM shirts WHERE LOCATE(CONCAT(',', 1 ,','),CONCAT(',',colors,',')) > 0; +----+--------------+--------------+ | id | ticketnumber | colors | +----+--------------+--------------+ | 1 | 32423 | 1,2,5,12,15 | | 2 | 32424 | 1,5,12,15,30 | | 4 | 32426 | 1,2,7,12,15 | +----+--------------+--------------+ 3 rows in set (0.00 sec)

    Give it a Try !!!

提交回复
热议问题