1

I have MySQL column Sizes which contains on every row data looking like this:

6 (39), 7 (40.5), 10 (44), 11 (45), 11½ (45.5), 12 (46)
6 (39), 7, 7 (40.5), 8 (42), 8½ (42.5), 9 (43), 10 (44.5), 11 (46)
6 (39), 7, 7 (40.5), 8 (42), 8½ (42.5), 9 (43), 11 (46)

I want to select from MySQL only the rows which contains specificly this 7,.

So if i have only three rows with data with the MySQL selection i have to get only the last two rows because they have 7, which is different from 7 (45),

Here is what i tried so far but somehow it is not working:

$Size = "7";
$Sizes = "$Size,";
$CategoryFilter = $_POST['CategoryFilter'];

$r = mysql_query("SELECT * FROM `products` WHERE `Category`='$CategoryFilter' AND `Sizes`  LIKE '%$Sizes%'");
$SDProductCode = array();
$Number = 0;
while($rowi = mysql_fetch_array($r))
        {
        $SDProductCode[] = $rowi['ProductCode'];
        $result['productcode'][] = $rowi['ProductCode'];
        $Number++;
        }

Why my selection is not working, can you help me out ?

Thanks in advance!

Venelin
  • 2,905
  • 7
  • 53
  • 117
  • normalize the data, problem solved –  Dec 20 '15 at 20:26
  • maybe interesting, if you want to `normalize` the table then [joining on ';' separated values in a column](http://stackoverflow.com/a/33806675/3184785) – Ryan Vincent Dec 20 '15 at 23:52

1 Answers1

0

Having white spaces around the commas, but still, with a bit of manipulation, find_in_set should do the trick:

SELECT *
FROM   `products` 
WHERE  `Category` = '$CategoryFilter' AND 
       FIND_IN_SET(REPLACE('$Sizes', ' ', ''), REPLACE(`Sizes`, ' ', '')) > 0

Note: This should work if you're searching for '7', not '7,'. I'm guessing that using the comma there was a workaround in the first place, so this may be acceptable.

Mureinik
  • 297,002
  • 52
  • 306
  • 350