0

It is very difficult to ask this question without giving a proper scenario, so here it goes. I have a MySQL database table with a field that stores an array of numbers. Like so...

id - values
1  - 1,2,3

I need to query against this. Like...

select * from table where values = 3

How to accomplish this?

If this was answered, please point me to the answer because I was unable to find it.

Please note that this is pure MySQL

D-Shih
  • 44,943
  • 6
  • 31
  • 51

2 Answers2

1

You can try to use FIND_IN_SET function.

CREATE TABLE `table`(
  id int,
  `values` varchar(50)
);


 insert into `table` values (1 ,'1,2,3');

Query 1:

select * from `table` where FIND_IN_SET('3',`values`) 

Results:

| id | values |
|----|--------|
|  1 |  1,2,3 |
D-Shih
  • 44,943
  • 6
  • 31
  • 51
0

You could use a regex like this:

SELECT * FROM `table` WHERE `values` RLIKE '(^|,)3($|,)'