2016-05-20 153 views
0

我想下面的SQL查询转换为Laravel雄辩或查询复杂Laravel查询

SELECT 
    SUM(T1.total) AS Total, 
    SUM(T1.received) AS Received, 
    T1.EventName, 
    T1.CurrencyType 
FROM 
    (
     SELECT 
      event_invoice.Id, 
      event_invoice.Amount AS total, 
      SUM(payments.recieved_amount + payments.adjust_amount) AS received, 
      event_invoice.EventName, 
      event_invoice.CurrencyType 
     FROM 
      event_invoice 
     LEFT JOIN payments ON event_invoice.Id = payments.invoice_id 
     GROUP BY 
      event_invoice.Id 
     ORDER BY 
      event_invoice.Id 
    ) T1 
GROUP BY 
    T1.EventName, 
    T1.CurrencyType 

我想转换成Laravel任何想法?

+0

为什么?为什么不创建一个视图,然后你没有任何复杂的与PHP搞乱这个。 – Mjh

回答

2

尝试这样:

$results = \DB::select(\DB::raw(" 
    SELECT 
     SUM(T1.total) AS Total, 
     SUM(T1.received) AS Received, 
     T1.EventName, 
     T1.CurrencyType 
    FROM 
     (
      SELECT 
       event_invoice.Id, 
       event_invoice.Amount AS total, 
       SUM(payments.recieved_amount + payments.adjust_amount) AS received, 
       event_invoice.EventName, 
       event_invoice.CurrencyType 
      FROM 
       event_invoice 
      LEFT JOIN payments ON event_invoice.Id = payments.invoice_id 
      GROUP BY 
       event_invoice.Id 
      ORDER BY 
       event_invoice.Id 
     ) T1 
    GROUP BY 
     T1.EventName, 
     T1.CurrencyType 
"));