0

我将复选框值作为逗号分隔值存储在数据库中,例如

Features

1,3,4

1,2,3,4,5

3,4,5,6

现在在搜索时我需要根据用户选择复选框来选择记录,例如如果用户选择1 and 6需要获取匹配的记录。我将选中的复选框值作为数组,如(0=>1,1=>6). 我不知道如何像这样循环FIND_IN_SET(1,Features) OR FIND_IN_SET(6,Features)

它必须像

SELECT * FROM table WHERE FIND_IN_SET(1,Features) OR FIND_IN_SET(6,Features)
4

2 回答 2

1
$str='%';
foreach($array as $val)
{$str.=$val.'%';}

SELECT * FROM table WHERE Features like $str

更新:

$str='';
    foreach($array as $val)
    {$str.='or Feature like %'.$val.'%';}
$str[0]='';$str[1]='';

    SELECT * FROM table WHERE $str
于 2012-07-10T04:41:17.313 回答
0

我使用下面的代码修复了这样的查询SELECT * FROM table WHERE FIND_IN_SET(1,Features) OR FIND_IN_SET(6,Features)

if($selCount >1){
    foreach($luxPropFeatures as $val) {
      $str .="OR FIND_IN_SET('".$val."',LP.luxury_property_feature_id) ";
      $trimmed = ltrim($str, "OR"); 
      //$str[0]='';$str[2]='';
     }
}else{

    foreach($luxPropFeatures as $val) {
        $trimmed = "FIND_IN_SET('".$val."',LP.luxury_property_feature_id) ";
    }

}
于 2012-07-10T11:01:49.603 回答