2017-03-28 49 views
0

我在覈心php中有一個查詢,但我想轉換成Laravel,我正在嘗試這樣做,但那是給出錯誤。我不知道,我可以利用這個 這裏的確切方法的核心查詢如何在laravel中使用聯合查詢

select mon,year, 
     combo, 
     registered, 
     forwardedbycmo, 
     clarification, 
     noaction, 
     disposed,mon_srno,undertaken 
from monthly_activities 
union 
select extract('month' from actiondate) as mon,extract('year' from actiondate) as year, 
     extract('year' from actiondate)|| '-' ||to_char(to_timestamp (extract('month' from actiondate)::text, 'MM'), 'TMMon') as combo, 
     sum(case when actioncode = '00' then 1 else 0 end) as registered, 
     sum(case when actioncode = '4T' and fromorg='CMOFF' then 1 else 0 end) as forwardedbycmo, 
     sum(case when actioncode = '4D' and fromorg='CMOFF' then 1 else 0 end) as clarification, 
     sum(case when actioncode = '10' then 1 else 0 end) as noaction, 
     sum(case when actioncode = '50' then 1 else 0 end) as disposed,null as mon_srno, 
     sum(case when actioncode = '40' and fromorg='CMOFF' then 1 else 0 end) as undertaken 

from actionhistory where extract(month from actiondate)=extract(month from current_date) 
and extract(year from actiondate)=extract(year from current_date) 
group by mon,year order by year,mon; 

和laravel查詢

$first = MonthlyActivity::select('mon','year','combo','registered','forwardedbycmo', 
     'clarification', 
     'noaction', 
     'disposed','mon_srno','undertaken')->get(); 

     $second = MonthlyActivity::select(extract('month' from actiondate) as mon,extract('year' from actiondate) as year, 
     extract('year' from actiondate)|| '-' ||to_char(to_timestamp (extract('month' from actiondate)::text, 'MM'), 'TMMon') as combo, 
     sum(case when actioncode = '00' then 1 else 0 end) as registered, 
     sum(case when actioncode = '4T' and fromorg='CMOFF' then 1 else 0 end) as forwardedbycmo, 
     sum(case when actioncode = '4D' and fromorg='CMOFF' then 1 else 0 end) as clarification, 
     sum(case when actioncode = '10' then 1 else 0 end) as noaction, 
     sum(case when actioncode = '50' then 1 else 0 end) as disposed,null as mon_srno, 
     sum(case when actioncode = '40' and fromorg='CMOFF' then 1 else 0 end) as undertaken 

from actionhistory where extract(month from actiondate)=extract(month from current_date) 
and extract(year from actiondate)=extract(year from current_date))->groupBy('mon','year')->orderBy('year','mon')->get(); 
$result = $first->union($second); 

//$users = DB::table('users')->whereNull('last_name')->union($first)->get(); 
     return view('show',['monthly' => $result]); 
+0

這是一個非常醜陋的查詢..我真的認爲它可以通過更改db架構來簡化..你應該考慮.. –

+0

@MateiMihai:我無法改變db結構tat已經被我詛咒了。請讓我知道是否有任何方法來執行這個查詢,因爲它是在laravel? –

+0

我對你的建議是重構這個查詢構建器,並使其看起來更好:) – Beginner

回答

0

根據你的問題的標題,here是聯合參考Laravel。 但看到你的問題,因爲你已經使用原生的SQL函數,如extract(),我會建議你運行查詢作爲原始查詢。

對於擡起頭來:

您使用:$var = DB::connection()->getPdo()->quote($var); 逃脫你正在使用的查詢任何變量。

準備好您的原始查詢,並把它像一個變量:

$query = 'SELECT ....'; 

然後運行它想:

$result = DB::select($query,[]); 

不要忘了包括DB類在控制器的頂部:

use Illuminate\Support\Facades\DB; 

對不起,我不能給你很多時間來爲你做一個laravel查詢,所以我在這裏建議whi ch可能會幫助你繼續下去。祝你好運!

+0

$ var = DB :: connection() - > getPdo() - > quote($ var);我可以在控制器下使用這條線嗎? –

+0

沒問題。如果通過接受堆棧溢出的答案解決了問題,請考慮關閉此線程。謝謝。 – Learner

+0

$ var = DB :: connection() - > getPdo() - > quote($ var);我可以在控制器下使用這條線嗎? –