douxitao8170 2015-12-20 20:21
浏览 45

PHP / MySQL - 当确切发生时在行中查找

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!

  • 写回答

1条回答 默认 最新

  • douwen1901 2015-12-20 20:30
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序