如何在laravel中使用联合查询

时间:2017-03-28 07:08:53

标签: php laravel-5

我在核心php中有一个查询,但我想转换成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]);
  

和laravel查询

        //Your folder path
        string yourFolderPath= @"C:/Folder/UnderFolder/Cat/Pics";
        //If you want to count number of folders in the given folder path
        int FolderCount = yourFolderPath.Count(s => s == '/');
        //If you want to create folder you can use directly below code
        Directory.CreateDirectory(yourFolderPath);

1 个答案:

答案 0 :(得分:0)

根据您的问题标题,here是Laravel的工会参考。 但是当您使用extract()等本机sql函数时看到问题,我建议您将查询作为原始查询运行。

提醒:

您使用:$var = DB::connection()->getPdo()->quote($var); 转义您在查询中使用的任何变量。

准备原始查询并将其放在如下变量中:

$query = 'SELECT ....';

然后你运行它:

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

不要忘记在控制器顶部包含DB类:

use Illuminate\Support\Facades\DB;

对不起,我不能给你很多时间来为你做一个laravel查询,所以我在这里建议哪些可以帮你继续。祝你好运!