2016-10-13 1 views
0

죄송합니다. 제 질문의 제목이 명확하지 않지만이 질문에 대한 답변을 찾을 수 없습니다.쿼리 작성기가 실행되지 않습니다. where = 다른 열에

다음 쿼리 빌더 코드가 있습니다.

return self::select(DB::raw('sum(user_points.points) AS points, users.id AS user_id, users.username, users.avatar, users.firstname, users.lastname')) 
      ->join('users', 'users.id', '=', 'user_points.user_id') 
      $query->where('user_points.artist_id', 0)->orWhere('user_points.artist_id', 'users.favourite_artist_id'); 
      }) 
      ->where('user_points.created_at', '>=', $startDate) 
      ->where('user_points.created_at', '<=', $endDate) 
      ->groupBy('users.id') 
      ->orderBy('points', 'DESC') 
      ->orderBy('user_id', 'ASC') 
      ->simplePaginate(100); 

그것은 구체적으로이 부분을 무시하는 것, 확인을 실행하지만, 내부 어디에 쿼리를 무시하다;

$query->where('user_points.artist_id', 0)->orWhere('user_points.artist_id', 'users.favourite_artist_id'); 
       }) 

그것은 'user_points.artist_id = 0'일치,하지만 그것은 일치하지 않는 년대 'user_points.artist_id = users.favourite_artist_id', 그것이 바인딩을 처리하는 것 길을 함께 할 수있는 뭔가있어 상정? 그러나 나는 그것이 작동하도록하는 방법을 찾을 수없는 것 같습니다.

전체 쿼리는 다음과 같이 끝납니다.

SELECT SUM(user_points.points) AS points, users.id AS user_id, users.username, users.avatar, users.firstname, users.lastname 
      FROM user_points 
      INNER JOIN users ON users.id = user_points.user_id 
      WHERE (user_points.artist_id = 0 OR user_points.artist_id = users.favourite_artist_id) 
       AND user_points.created_at >= '$startDate' AND user_points.created_at <= '$endDate' 
      GROUP BY user_id 
      ORDER BY points DESC, user_id ASC 

이 쿼리 빌더 코드를 업데이트했습니다.

return self::select(DB::raw('sum(user_points.points) AS points, users.id AS user_id, users.username, users.avatar, users.firstname, users.lastname')) 
      ->join('users', 'users.id', '=', 'user_points.user_id') 
      ->where(function($query) { 
        $query->Where('user_points.artist_id', 0)->orWhere(DB::raw('user_points.artist_id = users.favourite_artist_id')); 
      }) 
      ->where('user_points.created_at', '>=', $startDate) 
      ->where('user_points.created_at', '<=', $endDate) 
      ->groupBy('users.id') 
      ->orderBy('points', 'DESC') 
      ->orderBy('user_id', 'ASC') 
      ->simplePaginate(100); 

마지막 쿼리가 끝난 것처럼 작동하지 않았습니다.

select sum(user_points.points) AS points, users.id AS user_id, users.username, users.avatar, users.firstname, users.lastname 
from `user_points` inner join `users` on `users`.`id` = `user_points`.`user_id` where (`user_points`.`artist_id` = ? or user_points.artist_id = users.favourite_artist_id is null) 
and `user_points`.`created_at` >= ? 
and `user_points`.`created_at` <= ? 
group by `users`.`id` 
order by `points` desc, `user_id` asc 
+0

당신이 ( '$ 질의 -> 여기서 (기능을 시도해 봤어 :

user_points.artist_id = 'users.favourite_artist_id' 

다음 시도해보십시오 곳 방법은 실제로 다음과 같은 노력되도록 값으로 두 번째 열을 추가합니다 $ 쿼리) { \t $ 질의 -> orWhere ('user_points.artist_id', 0) \t \t \t \t -> orWhere ('user_points.artist_id', 'users.favourite_artist_id'); })' –

+0

예 그걸 시도했지만 결과는 같습니다. – SheppardDigital

답변

1

원시 쿼리로 두 번째 (또는)를 추가해야합니다.

whereRaw("user_points.artist_id = users.favourite_artist_id") 
+0

예를 들어, 두 번째 orWhere를 원시 쿼리로 추가하려고했으나 최종 쿼리가 올바르지 않습니다. – SheppardDigital

+0

완벽한, 감사합니다. – SheppardDigital

관련 문제