how to select from filed array

Hi ,

U have this table structure


id        |    name         |    tags         +


1         |   test1         |  3,4,33,54      +

2         |   test2         |  1,3,63,11      +


i want to get all the records that have 3 in its tag fields.

So i tried this :

// $tag = 3

$query->andFilterWhere(['like', 'tags', $tag]);

the problem its include any 3 in the record such as 23 , 33 , 63 .

how can i get only the record that has 3 in tag field ?

thank you

If you can modify the database schema, just get rid of this "comma separated values" hack and create a proper has-many or many-to-many relation instead. It will pay off in the long run.

If it’s a legacy production database and you aren’t allowed to modify the schema, try to use a regular expression search (REGEXP operator in MySQL) but it should be the last resort because it’s really slow and unreliable.