2016-06-07 51 views
0

我有一個查詢允許我過濾一些項目。MYSQL/Laravel:將WHERE轉換爲變量

你不必理解它來幫助我。

$payments->whereExists(function($query) use ($startDate, $endDate){ 
        // We must browse the projects to know if there was a project created BEFORE the project^and with different id 

        $query->select(DB::raw(1)) 
         ->from('project as oldProjects') 
         ->whereRaw('oldProjects.deleted = 0') 
         ->whereRaw('oldProjects.id != project.id') 
         ->whereRaw('oldProjects.date_entered < project.date_entered') 

         ->whereExists(function($query2){ 
          // We have to check if the project found belongs to the specified account 
          $query2->select(DB::raw(1)) 
          ->from('accounts_project_1_c as oldProjectsAccount') 
          ->where('oldProjects.deleted', 0) 
          ->whereRaw('oldProjectsAccount.accounts_project_1project_idb = oldProjects.id') 
          ->whereRaw('oldProjectsAccount.accounts_project_1accounts_ida = accounts_pm7_payments7_1_c.accounts_pm7_payments7_1accounts_ida'); 
         }); 
       }); 

因此,這是一個WHERE,這將幫助我從返回客戶端獲得項目(之前的項目存在)。

我想添加到另一個請求,但作爲一個值(返回= 1,或返回= 0),不再作爲一個WHERE。

僅供參考,這裏是我想補充這一變量的要求:

$trustLogsQuotes = DB::table('trlog_trustaccountlog') 
      ->select(
       'trlog_trustaccountlog.id as trlog_id', 
       'quotes.id as quote_id', 
       'quotes.name as quote_name', 
       'quotes_cstm.quote_type_c') 
      ->leftJoin('trlog_trustaccountlog_project_c', 'trlog_trustaccountlog_project_c.trlog_trustaccountlog_projecttrlog_trustaccountlog_idb', '=', 'trlog_trustaccountlog.id') 
      ->leftJoin('project', 'trlog_trustaccountlog_project_c.trlog_trustaccountlog_projectproject_ida', '=', 'project.id') 
      ->leftJoin('project_quotes_1_c', 'project_quotes_1_c.project_quotes_1project_ida', '=', 'project.id') 
      ->leftJoin('quotes', 'quotes.id', '=', 'project_quotes_1_c.project_quotes_1quotes_idb') 
      ->leftJoin('quotes_cstm', 'quotes_cstm.id_c', '=', 'quotes.id') 
      ->where('trlog_trustaccountlog.transfer_date_c', '>=', $startDate) 
      ->where('trlog_trustaccountlog.transfer_date_c', '<', $endDate) 
      ->where('trlog_trustaccountlog.transfer_type_c', 'Transfer') 
      ->where('trlog_trustaccountlog.deleted', 0) 
      ->where('trlog_trustaccountlog_project_c.deleted', 0) 
      ->where('project.deleted', 0) 
      ->where('project_quotes_1_c.deleted', 0) 
      ->where('quotes.deleted', 0) 
      ->get(); 

我真的不知道如何將它轉換。你有什麼想法我可以做到這一點?

+0

我不知道,如果你想重用在兩個地方,其中部分或這是一個數據庫相關的問題,你想在生成的SELECT語句一樣的東西,如果-X-THEN-1。 – herrjeh42

+0

這是第二種選擇,if-x-then-1,但邏輯當前設置在WHERE中(請參閱第一個請求) – Vico

+0

Mysql支持IF和CASE-WHEN語句,請參閱http://dev.mysql.com/ doc/refman/5.7/en/control-flow-functions.html 這就是你如何使用Laravel: $ query-> select(「operations。*」,\ DB :: raw(「SUM(if (interviews.valid = 2,1,0))AS invalid「)); – herrjeh42

回答

0

感謝評論,這是我的工作成果。

DB::raw('IF (EXISTS (
           SELECT 1 FROM project as oldProjects 
           WHERE oldProjects.deleted = 0 
           AND oldProjects.id != project.id 
           AND oldProjects.date_entered < project.date_entered 
           AND EXISTS(
             SELECT 1 FROM accounts_project_1_c as oldProjectsAccount 
             WHERE oldProjects.deleted = 0 
             AND oldProjectsAccount.accounts_project_1project_idb = oldProjects.id 
             AND oldProjectsAccount.accounts_project_1accounts_ida = account_id 
            ) 
           ), 1, 0) as returning'),