0

when run laravel DB::select with IN statment, as documented:

//$list_of_ids = '1,3,5,66,3'
//$uid = 1

DB::select('
    SELECT SUM(score)
    FROM user
    WHERE id
    IN (?)
    and user_id = ?
    LIMIT 4
', array($list_of_ids,$uid));

not receive any results

but when run:

DB::select('
    SELECT SUM(score)
    FROM user
    WHERE id
    IN ('.$list_of_ids.')  //add var here
    and user_id = ?
    LIMIT 4
', array($uid));    

return correct results, what is the matter in first way?

mwafi
  • 3,946
  • 8
  • 56
  • 83
  • Here's an explanation why this doesn't work [http://stackoverflow.com/questions/920353/can-i-bind-an-array-to-an-in-condition](http://stackoverflow.com/questions/920353/can-i-bind-an-array-to-an-in-condition). The short version is that it isn't related to Laravel, but PDO itself. – kajetons Aug 24 '14 at 07:54

1 Answers1

0

What you need to to is use an actual array and a proper Query.

$userIds = explode(',', $list_of_ids);
DB::table('user')->whereIn('id', $userIds)->where('user_id', $uid)->limit(4)->get([DB::raw('SUM(score)')]);
Marwelln
  • 28,492
  • 21
  • 93
  • 117