2017-04-10 1 views
0

지난 2 시간 동안 Laravel Query Builder를 사용하여 만든 MySQL 쿼리에 어려움을 겪고 있습니다.하지만 매개 변수가 무시되는 이유를 이해하지 못합니다.Laravel 쿼리 작성기 : 예기치 않은 출력

select * from `deals` where 
(
    ( 
     `created_at` <= '2017-04-10 09:44:07' 
     and `valid_until` >= '2017-04-10 09:44:07' 
    ) or (
     `created_at` <= '2017-04-10 09:44:07' 
     and `valid_until` <= '2017-04-10 09:44:07' 
     and `autodelete` = '2' 
    ) or (
     `created_at` <= '2017-04-10 09:44:07' 
     and `valid_until` <= '2017-04-10 09:44:07' 
     and `autodelete` = '0' 
    ) or (
     `created_at` <= '2017-04-10 09:44:07' 
     and `valid_until` is null 
    ) 
    and `category_id` != '1' 
) and `deals`.`deleted_at` is null 
order by `order_date` desc, `created_at` desc 
limit 20 
offset 0 

CATEGORY_ID의 필터링입니다 무시되고있는 부분 : 쿼리 빌더에서 나오는

쿼리는 다음과 같다. category_id 1이있는 항목은 제외해야하지만 제외되지는 않습니다. 그리고 나는 왜 그런지 이해하지 못합니다.

이제 나는 그 부분을 괄호 밖으로 옮기면 작동한다는 것을 알았습니다. 유일한 문제는 다음과 같습니다. 쿼리에 괄호를 추가하지 않고 있습니다. Laravel이 소프트 삭제를 사용하고 있기 때문에 자동으로 그렇게하는 것 같습니다.

내가 쿼리를 만드는 데 사용하고 코드입니다 :

 $query = Deal::query() 
     ->where(function ($query) { 
      // Active with an end date 
      $query->where('created_at', '<=', date("Y-m-d H:i:s")) 
      ->where('valid_until', '>=', date("Y-m-d H:i:s")); 
     }) 
     ->orwhere(function ($query) { 
      // Ended, but with possibility to upload a receipt 
      $query->where('created_at', '<=', date("Y-m-d H:i:s")) 
      ->where('valid_until', '<=', date("Y-m-d H:i:s")) 
      ->where('autodelete', 2); 
     }) 
     ->orwhere(function ($query) { 
      // Ended, but keep it online 
      $query->where('created_at', '<=', date("Y-m-d H:i:s")) 
      ->where('valid_until', '<=', date("Y-m-d H:i:s")) 
      ->where('autodelete', 0); 
     }) 
     ->orwhere(function ($query) { 
      // No end date 
      $query->where('created_at', '<=', date("Y-m-d H:i:s")) 
      ->where('valid_until', null); 
     }); 

// Filter categories 
if(!empty($deselectedCategories)) 
{ 
    foreach($deselectedCategories as $key => $val) 
    { 
     $query->where('category_id', '!=', $val); 
    } 
} 

$deals = $query->orderBy('order_date', 'desc')->orderBy('created_at', 'desc')->paginate($resultsPerPage); 

내가 뭔가를 내려다해야하며, 나는 누군가가 나를 도울 수 있기를 바랍니다. 대단히 감사드립니다!

답변

0

친절하게 내가

  1. 이를 제거하고 CATEGORY_ID 쿼리를 재구성 한 https://laravel.com/docs/5.4/queries#parameter-grouping

    그룹화하여 매개 변수에 가까운보세요! = {발}는

  2. 쿼리

    를 다시 그룹화 whereNotIn 사용
    $query = Deal::where(function 
         ($query){ 
          $query->where(function ($query) { 
                // Active with an end date 
           $query->where('created_at', '<=', date("Y-m-d H:i:s")) 
           ->where('valid_until', '>=', date("Y-m-d H:i:s")); 
          }) 
          ->orwhere(function ($query) { 
           // Ended, but with possibility to upload a receipt 
           $query->where('created_at', '<=', date("Y-m-d H:i:s")) 
           ->where('valid_until', '<=', date("Y-m-d H:i:s")) 
           ->where('autodelete', 2); 
          }) 
          ->orwhere(function ($query) { 
                // Ended, but keep it online 
           $query->where('created_at', '<=', date("Y-m-d H:i:s")) 
           ->where('valid_until', '<=', date("Y-m-d H:i:s")) 
           ->where('autodelete', 0); 
          }) 
          ->orwhere(function ($query) { 
                // No end date 
           $query->where('created_at', '<=', date("Y-m-d H:i:s")) 
           ->where('valid_until', null); 
          }); 
         }); 
    
         if (!empty($deselectedCategories)) { 
          $catList =array(); 
          foreach ($deselectedCategories as $key => $val) { 
           $catList[] = $val; 
          } 
         $query->whereNotIn('category_id', $catList); 
        } 
    $query->whereNull('deleted_at'); 
    $deals = $query->orderBy('order_date', 'desc')->orderBy('created_at', 'desc')->paginate($resultsPerPage); 
    
+0

감사합니다. 당신의 노력에 아주 많이 부합합니다. 이것은 훨씬 깨끗해 보입니다. 그러나 결과는 여전히 동일합니다. category_id는 여전히 MySQL에 의해 무시되고 있습니다. 위의 예에서 category_id 1이있는 항목은 출력에 계속 포함됩니다. –

+0

시도 할 샘플 데이터 소스가 있습니까 – kwaseth

+0

버전에서 나오는 쿼리는 다음과 같습니다. https://pastebin.com/xwsasXC2 왜 그런지 알 수는 없지만 출력이 다음과 같은 경우 작동합니다. https : //pastebin.com/nrj4FKBs 쿼리 작성기를 사용하여이 쿼리를 생성하지 못했습니다. 그것은 괄호 사이에 머문다. 그리고 제 의견으로는 왜 제대로 작동하지 않는지 이해하고 싶습니다. 사용중인 MySQL 테이블은 고급 설정이없는 단순한 테이블입니다. –