在字段PHP mysql查询中检查逗号分隔值

时间:2014-11-10 09:29:57

标签: php mysql sql phpmyadmin

我在一个名为filters的表中有一个字段,它可以有逗号分隔值1,2,3或3,1或更多。我只需要选择那些匹配这些记录的记录。假设有人发送1, 3,它必须存在于过滤器字段中,即操作。

我正在使用FIND_IN_SET并且它对单个值工作正常,假设有人发送1并且它正在获得正确的记录但是如果有人发送逗号分隔1,3则会出现以下错误

  

查询无效:调用本机时参数计数不正确   功能' FIND_IN_SET'

对于单个记录,我使用以下查询,它工作正常。

SELECT shoptitle,
        logopic,
        complocality, 
        profileurl, 
        lat, 
        lng,
        mageuserid,
        compdesi,
        deliverymin,
        deleiveryprice,
        deleiverymethod,
        vendorphone,
        compcity,
        compstate,
        zipcode,
        showcompleteaddress,
        ( 3959 * acos( cos( radians('33.8352932') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('-117.91450359999999') ) + sin( radians('33.8352932') ) * sin( radians( lat ) ) ) ) AS distance 
FROM userdata 
WHERE FIND_IN_SET (1,filter) 
HAVING distance < '10'   
ORDER BY distance

3 个答案:

答案 0 :(得分:0)

您必须使用以下代码检查该字符串是否包含逗号

sqlvalue="1,2,3,4";
   $HiddenProducts = explode(',',$sqlvalue);
if (in_array(1, $HiddenProducts)) {
  echo "Available";
}
if (in_array(3, $HiddenProducts)) {
  echo "available";
}

答案 1 :(得分:0)

最好的解决方案是规范化数据库,删除以逗号分隔的值列表。这些问题是很难检查多个值(正如您所发现的那样),但即使对于单个值,任何检查都不会使用索引,因此会很慢。

如果你没有需要拆分列表进行检查,然后在每个列表上使用FIND_IN_SET来检查它是否在现场。

这样的事情: -

$some_values = "1,3";

$some_values_array = explode(',', $some_values);

$Some_checks = "FIND_IN_SET ('".implode("',filter) AND FIND_IN_SET ('", $some_values_array).")";

$sql = "SELECT shoptitle,
            logopic,
            complocality, 
            profileurl, 
            lat, 
            lng,
            mageuserid,
            compdesi,
            deliverymin,
            deleiveryprice,
            deleiverymethod,
            vendorphone,
            compcity,
            compstate,
            zipcode,
            showcompleteaddress,
            ( 3959 * acos( cos( radians('33.8352932') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('-117.91450359999999') ) + sin( radians('33.8352932') ) * sin( radians( lat ) ) ) ) AS distance 
    FROM userdata 
    WHERE $Some_checks
    HAVING distance < '10'   
    ORDER BY distance";

严格地说,可以在SQL中执行此操作,但它会非常可怕且难以维护,同时也会变慢。

答案 2 :(得分:0)

  

在我的情况下,我做了以下解决方案:

        $filter = '1,2,3,4,5';
        $newbreak = explode(",",$filter);
        $checksize  = sizeof($newbreak);

        if ($checksize == 2)
        {
            $new = "FIND_IN_SET (".$newbreak[0].",filter) AND FIND_IN_SET (".$newbreak[1].",filter)";
        }elseif($checksize == 3)
        {
            $new = "FIND_IN_SET (".$newbreak[0].",filter) AND FIND_IN_SET (".$newbreak[1].",filter) AND FIND_IN_SET (".$newbreak[2].",filter)";
        }
        elseif($checksize == 4)
        {
            $new = "FIND_IN_SET (".$newbreak[0].",filter) AND FIND_IN_SET (".$newbreak[1].",filter) AND FIND_IN_SET (".$newbreak[2].",filter) AND FIND_IN_SET (".$newbreak[3].",filter)";
        }
        elseif($checksize == 5)
        {
            $new = "FIND_IN_SET (".$newbreak[0].",filter) AND FIND_IN_SET (".$newbreak[1].",filter) AND FIND_IN_SET (".$newbreak[2].",filter) AND FIND_IN_SET (".$newbreak[3].",filter) AND FIND_IN_SET (".$newbreak[5].",filter) ";
        }else
        {

        }
$query =
 sprintf("SELECT * ,( 3959 * acos( cos( radians('%s') ) * 
cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) 
* sin( radians( lat ) ) ) ) AS distance FROM userdata WHERE 
$new HAVING distance < '%s'   ORDER BY distance",
                    mysql_real_escape_string($center_lat),
                    mysql_real_escape_string($center_lng),
                    mysql_real_escape_string($center_lat),
                    mysql_real_escape_string($radius));