简体   繁体   中英

Find array wise ids from comma separated ids in mysql php

i have one table which contains multiple comma separated ids with multiple fields like:

在此处输入图片说明

i need some proid which i have already pass using array in mysql

proid = array([0]=>51,[1]=>54,[2]=>8)

i want that rows which contains 51,54 & 8 proid from the table field which is in comma separate proids

51,52,53,54,2,3,4,5,8,9,11,55,13,14,15,16,17,18,1,... 

here i try FIND_IN_SET but not work properly in select query, can see below

SELECT * FROM `tbl_request` WHERE FIND_IN_SET('51,54,8', `proid`) > 0

can anyone help me out please,

thanks in advance

FIND_IN_SET() function take first parameter as a complete string and compare it with the second parameters. If you pass 51 as first param then SQL will return you the results. But you try for '51,8' then it will treat it as a one string not two, and it will search for complete '51,8'. So you need to as Jothi mentioned in comments

SELECT FIND_IN_SET('51', `proid`) > 0 or FIND_IN_SET('54', `proid`) > 0 or FIND_IN_SET('8', `proid`) > 0; 

Reference: http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_find-in-set , http://www.w3resource.com/mysql/string-functions/mysql-find_in_set-function.php

$sql = 'SELECT * FROM `tbl_request` WHERE ';
$items = array(51,54,8);
foreach($items as $item){
    $where[] = "FIND_IN_SET('$item', `proid`) > 0";
}
if(is_array($where)){
    $sql.= implode(' OR ', $where);
}else{
    $sql.= '1';
}

FIND_IN_SET() only finds the position of the first argument.

You may try

SELECT
 *
FROM tbl_request
WHERE FIND_IN_SET('51', proid)
   OR FIND_IN_SET('54', proid)
   OR FIND_IN_SET('8', proid);

You can do this like below:

$proid = array(0=>51,1=>54,2=>8);

$cond_array = array() ;

foreach($proid as $val){
    $cond_array[] = "FIND_IN_SET($val, `proid`) > 0";
}

$sql = "SELECT * FROM `tbl_request` WHERE ".implode(' OR ', $cond_array);

echo $sql;

output :

SELECT SELECT * FROM `tbl_request` WHERE FIND_IN_SET(51, `proid`) > 0 OR FIND_IN_SET(54, `proid`) > 0 OR FIND_IN_SET(8, `proid`) > 0

try this

            <?php

        $proid = array(51,54,8);


        $concat='';

        foreach($proid as $row)
        {
          if(empty($concat))
          {
              $concat.=" FIND_IN_SET('$row', proid) >0";

          }
          else
          {

           $concat.=" or FIND_IN_SET('$row', proid) >0";
          }


        }

        echo "SELECT * FROM tbl_request WHERE ".$concat

        ?>

Guys for multiple fields selection i use this code and it working fine

$proids='';
        foreach($_POST["product"] as $row_product)
        {
          if(empty($proids))
          {
              $proids.=" FIND_IN_SET('$row_product', `proid`) >0";
          }
          else
          {
            $proids.=" OR FIND_IN_SET('$row_product', `proid`) >0";
          }
        }

        $disidreport='';
        foreach($_POST["distributor"] as $row_distributor)
        {
          if(empty($disidreport))
          {
              $disidreport.=" FIND_IN_SET('$row_distributor', `disidreport`) >0";
          }
          else
          {
            $disidreport.=" OR FIND_IN_SET('$row_distributor', `disidreport`) >0";
          }
        }

        echo $select_req = "SELECT * FROM `tbl_request` WHERE (".$disidreport.") AND (".$proids.")";
$sql = 'SELECT * FROM course where ';
$items = array(49,50,52);
foreach($items as $item){
    $where[] = "FIND_IN_SET(cid,'$item') > 0";
}
if(is_array($where)){
    $sql.= implode(' OR ', $where);

}else{
    $sql.= '1';

}
$statement = $dbo->prepare($sql);

$statement->execute();

$result = $statement->fetchAll();

  foreach($result as $row)
     {
      echo $sid=$row["cid"]."      ";
      echo $name = $row["cname"]."<br>";
     }
}

It will Work Like Charm Modified in the above statement I am not getting the result. but after changing in FIND_IN_SET I will get all the data.

在此处输入图片说明

That's statement right I am not telling that is wrong but Mistake is in FIND_IN_SET function of SQL . You can see in the picture what query will help me to get the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM