Mysql Rlike条件

时间:2015-03-20 05:23:00

标签: mysql rlike

我有一个表名company

Company  |  category_id
NEC      |     cg17,cg19,cg23
Huawei   |     cg55,cg17,cg25

我这样查询:

select * from company
where category_id RLIKE '^cg17,cg19'

结果:

Company  |  category_id
NEC      |  cg17,cg19,cg23

但是当我这样查询时:

select * from company
where category_id RLIKE '^cg17,cg23'

我没有行,但我想要公司“NEC”,因为NEC有category_id “cg17,CG19,CG23”

如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

一种方法是,如果您的类别总是有序且不包含超过2位数的类别:

select * from company
where category_id LIKE 'cg17%cg23'

更好的方式恕我直言,将该字段视为正确的CSV值:

select * from company
where FIND_IN_SET('cg17', category_id) > 0
and FIND_IN_SET('cg23', category_id) > 0