0

I have a table in sqlite named Devices which has following columns

Id, DeviceId, FavBrands, FavCategories

the sample data in it is

 1        50ea9017cb5ad            1,2,8,9           4,6,8
 2        50ead1683dcc0            3,7,9             4,5,9
 3        50ee73b56c4ea            1,2,7             6,7,8

I want to query this table to return deviceid which has FavBrands 7 in them relationg to table above it should give row number 2 and 3.

Thanks

1Mayur
  • 3,419
  • 5
  • 40
  • 64

1 Answers1

2

You may use this:

SELECT * FROM Devices
WHERE instr(','||FavBrands||',' , ',7,')>0;

(An obligatory remark about please normalizing your database, with a link to Is storing a delimited list in a database column really that bad?).

If you heed another advice and use LIKE, ensure you use '%,7,%' and add commas as we do here, to avoid false positives (e.g. with 77 in the list).

Community
  • 1
  • 1
Anton Kovalenko
  • 20,999
  • 2
  • 37
  • 69