Laravel 4:DB :: select with IN语句

时间:2014-08-24 06:52:19

标签: mysql laravel laravel-4

运行laravel DB :: select with IN语句,如记录所示:

//$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));

没有收到任何结果

但运行时:

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

返回正确的结果,第一种方式是什么问题?

1 个答案:

答案 0 :(得分:0)

您需要使用的是实际数组和正确的查询。

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