简体   繁体   中英

retrieve Comma separated values in MySQL php with LIKE

Array
(
    [0] => Array
        (
            [id] => 1
        )

    [1] => Array
        (
            [id] => 16
        )

    [2] => Array
        (
            [id] => 21
        )

    [3] => Array
        (
            [id] => 22
        )

    [4] => Array
        (
            [id] => 2
        )
)

above array is an $id

$grp_id="";
foreach($id as $gid)
{
   $grp_id .=  " LIKE %".$gid['id']."% AND rj.applyto";
}

my query is

$sql="SELECT * FROM `table` j INNER JOIN `table2` rj ON j.eid = rj.eid WHERE rj.applyto LIKE $grp_id";

i did an mistake in my auery pls suggest me to coorect..

Do you really need to use LIKE here? 1 and 2 are so common I don't think it is what you want.

Assuming that is not what you really want, just use IN() . Much simpler.

// Create comma separated list of IDs
$ids = implode(',', $id);
// Use IN() in our WHERE clause
$sql="SELECT * FROM `table` j INNER JOIN `table2` rj ON j.eid = rj.eid WHERE rj.applyto IN($ids);

You just need to implode your ids with comma separated and use FIND_IN_SET for this like:

$ids = implode(',', $id_array);
$sql="SELECT * FROM `table` j INNER JOIN `table2` rj ON j.eid = rj.eid WHERE FIND_IN_SET(rj.applyto, $ids)";

Try this!

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