首先我们建立一张带有逗号分隔的字符串。
CREATE TABLE test(id int(6) NOT NULL AUTO_INCREMENT,PRIMARY KEY (id),pname VARCHAR(20) NOT NULL,pnum VARCHAR(50) NOT NULL);
然后插入带有逗号分隔的测试数据
INSERT INTO test(pname,pnum) VALUES('产品1','1,2,4');
INSERT INTO test(pname,pnum) VALUES('产品2','2,4,7');
INSERT INTO test(pname,pnum) VALUES('产品3','3,4');
INSERT INTO test(pname,pnum) VALUES('产品4','1,7,8,9');
INSERT INTO test(pname,pnum) VALUES('产品5','33,4');
查找pnum字段中包含3或者9的记录
mysql> SELECT * FROM test WHERE find_in_set('3',pnum) OR find_in_set('9',pnum);
+----+-------+---------+
| id | pname | pnum??? |