1
votes

I have a sql table called "Offres"
inside this table a column named "regions" has array in it (value1, value2, value3,...)
row1 :"35,2,15,69,98"
row2 :"7,9,15,5,69"
row3 :"7,3,45,5,6"
how can I search rows which has multiple value in same time
for exp : I want to search rows which has 15,69 in it
the result should display row1 and row2
thanks

enter image description here

5
you can use like in your sql query - Deepanshu Goyal
is this a set column or an actual varchar with comma seperated values? for the latter you could use a like-comparison in the select statement otherwise look here for operations on set-columns dev.mysql.com/doc/refman/5.1/en/set.html - DrCopyPaste
You could try it with regular expressions (which would also fix all of the problems described with LIKE below), but this will slow down your queries. Convert your table structure like juergen d described it and learn something about database normalization. This will solve your problem. - Reeno

5 Answers

-2
votes

for exp : I want to search rows which has 15,69 in it


if you can add spaces before and after your values, then it might be easy check fiddle

like

row1 :' 690 , 6969 , 069 , 69069 '
row2 :' 69 , 69 , 69 , 69 , 69 '
row3 :' 6969 , 69 , 69069 '
row4 :' 069 , 69069 '
row5 :' 69 '

    SELECT * FROM Offres WHERE regions LIKE regions LIKE '% 69 %';
5
votes

Never, never, never store multiple values in one column!

As you see now this will only result in problems. Please normalize your DB structure first like this

Offers
id   region
1    35
1    2
1    15
...
2    7
2    9
2    15
...
0
votes

If you intend for a field to be searchable via SQL, store it as multiple rows and do not serialize it.

If some reasons, you have to, you may have to extract out each row, deserialize it (for your case, with explode(), and match it singularly, which takes up lots of processing power.

0
votes

You can search for elements within such lists with FIND_IN_SET(). If you need to find multiple entries within the rows, you need multiple selects.

But: Good database structures would avoid all this and have only one entry per row instead of lists

-1
votes

For the sake of completeness I am posting this as an answer, too, so it does not get lost in the huge discussion in the comments ;)

First of all, if you want to have multiple values in one column in SQL the set-column is the way to go. See for example here and here. But if you absolutely cannot get around having multiple values in one column and you can guarantee, that they will always occur in the same pattern (comma-separated, no spaces), then you can search for rows that contain 2 specific such values you can do it like this (according to your example)

SELECT * FROM Offres WHERE
  (
      regions = '15' /*value is the only one in that row, obviously this is not necessary if you look for 2 values at once, but if you would only look for one value at once you MUST include it ;)*/
    OR regions LIKE '%,15' /*value is the last value in that row*/
    OR regions LIKE '15,%' /*value is the first value in that row*/
    OR regions LIKE '%,15,%' /*value is a value somewhere in between other values in that row*/
  )
  AND
  (
      regions = '69' /*value is the only one in that row, obviously this is not necessary if you look for 2 values at once, but if you would only look for one value at once you MUST include it ;)*/
    OR regions LIKE '%,69' /*value is the last value in that row*/
    OR regions LIKE '69,%' /*value is the first value in that row*/
    OR regions LIKE '%,69,%' /*value is a value somewhere in between other values in that row*/
  )

You can also try that out in this fiddle.