2016-09-23 58 views
-1

我有我的老闆任務使用laravel 5. 顯示來自MySQL的數據,他給我這個查詢SQL使用distinct與連接表的Mysql laravel 5

SELECT DISTINCT loans.loan_number, borrower.brate, loans.loint, loan_offer.amount, 
loans.lterm,SUBSTRING(repayment_lender.repay_due_date,4,2) 
FROM loans, loan_offer, borrower, repayment_lender,LENDER 
WHERE loans.id = loan_offer.loan_id 
AND loans.borrower_id = borrower.id 
AND repayment_lender.loans_id = loans.id 
AND lender.id = loan_offer.lender_id 
AND lender.id = 3 

如果執行查詢它的外觀這樣Like this

我用這個代碼在我的控制器

$data_loanoffers = DB::table('loans')->join('loans', 'loans.id', '=', 'repayment_lender.loans_id') 
              ->join('loans', 'loans.id', '=', 'loan_offer.loan_id') 
              ->join('borrower', 'borrower.id', '=', 'loans.borrower_id') 
              ->join('loan_offer', 'loan_offer.lender_id', '=', 'lender.id') 
              ->where('lender.id',$l_id)->distinct()->get(); 
return view('account.investor_portfolio',[ 
      'data_loanoffers' => $data_loanoffers, 
]); 

,並使用此代碼在視圖中顯示數據012

在前端
@foreach($data_loanoffers as $loans)  
            <tr> 
             </td> 
             <td>{{$loans->loan_number}}</div> 
             <td> {{$borrower->brate}} </div> 
             <td> {{$loans->loint}} </div> 
             <td> {{$loan_offer->amount}}</div> 
             <td> {{$loans->lterm}}</div> 
             <td> ?? </div> 
            </tr> 

            @endforeach 

顯示eror LIKE THIS

我在laravel 5.新手,我不知道我的代碼錯誤。也許這裏的高手可以幫助我嗎? 在此先感謝。

+0

解決我的解決方案所以..會發生什麼?你需要告訴我們你的問題。你得到錯誤的結果?錯誤消息?代碼變得有感覺,專注於世界統治? –

+0

SQLSTATE [42000]:語法錯誤或訪問衝突:1066不唯一表/別名:'loans'(SQL:選擇不同的'loan'.'loan_number','borrower'.'brate','loans'.loint' ,'loan_offer'.'amount','loans'.'lterm','SUBSTRING(repayment_lender'.'repay_due_date,4,2)''''''''''''''''''''''''貸款''''貸款''''貸款'''貸款'''''貸款'''內部連接'借貸者' 'loan_offer' on'loan_offer'.'lender_id' ='lender'.'id' where'lender'.'id' = 1) –

+0

在我的前端有這樣的錯誤 –

回答

0

我只是通過查詢構建器laravel

$data_loanoffers  = DB::select(DB::raw("SELECT DISTINCT loans.loan_number, borrower.brate, loans.loint, loan_offer.amount,loans.lterm,SUBSTRING(repayment_lender.repay_due_date,4,2) as DueDate 
            FROM loans, loan_offer, borrower, repayment_lender,LENDER 
            WHERE loans.id = loan_offer.loan_id 
            AND loans.borrower_id = borrower.id 
            AND repayment_lender.loans_id = loans.id 
            AND lender.id = loan_offer.lender_id 
            AND lender.id = '$l_id' ")); 
0

您需要選擇某個列,以便可以將不同的字段應用於唯一字段。