如何将此SQL查询转换为laravel查询生成器?

时间:2019-04-18 07:50:15

标签: mysql laravel-5 select group-by laravel-query-builder

我有一个表( itemregistrationpangkat ),其名称(与其他表项的注册表结合),negeri(与其他表的negeri结合)以及开始服务和结束服务的年份。我想获得一个人在某州服务的年限:

  ID | Name     | negeri   |   yearstart     |    yearend
  -----------------------------------------------------------
  1  | Tom      | Kedah   |      2001       |      2002
  1  | Tom      | Kedah   |      2003       |      2007
  2  | Anne     | Melaka  |      2008       |      2012
  2  | Anne     | Melaka  |      2013       |      2018
  3  | Bill     | KL      |      2000       |      2001

我不得不问某人如何制作一条用于查询信息的SQL语句。这是一条SQL语句,其用法类似于小提琴http://sqlfiddle.com/#!9/9029438/4(this,小提琴假定negeri和name列在同一表中):

  select m.id,m.name,m.state,sum(m.duration) 
    from (select ID,Name,state,yearend-yearstart as duration from itemregistrationpangkat)m 
    group by  m.id,m.state,m.name;

我需要将sql语句修改为具有原始DB结构的laravel sql格式。我尝试了以下代码。它很长,因为它使用if语句搜索函数:

  $query = DB::table('itemregistrations')
                ->join('sections', 'itemregistrations.SectionID', '=', 'sections.SectionID')
                ->join('categories', 'itemregistrations.CategoryID', '=', 'categories.CategoryID')
                ->join('operasi', 'itemregistrations.OperasiID', '=', 'operasi.OperasiID')
                ->join('negeri', 'itemregistrations.NegeriID', '=', 'negeri.NegeriID')
                ->join('gred', 'itemregistrations.GredID', '=', 'gred.GredID')
                ->where('itemregistrations.statusProID', '=', 1)
                ->select('itemregistrations.name','sections.sectionname', 'categories.categoryname', 'operasi.operasiname', 'itemregistrations.Nobadan', 'itemregistrations.lahir_yy', 'itemregistrations.pdrm_yy', 'gred.namagred', 'itemregistrations.itemRegistrationID');

  if($request->input('negeri_perkhidmatan') != '') {
            $query->join('itemregistrationpangkat', 'itemregistrationpangkat.itemRegistrationID', '=', 'itemregistrations.itemRegistrationID')
                ->where('itemregistrationpangkat.NegeriID', $request->input('negeri_perkhidmatan'));           
        }

  if(request('tempoh_negeri')) {

      $query->select(DB::raw("m.ItemRegistrationID, sum(m.duration) from (SELECT itemregistrationpangkat.itemRegistrationID, itemregistrationpangkat.yeartamatkhidmat - itemregistrationpangkat.yearmulakhidmat as duration FROM itemregistrationpangkat) as m"))
      ->groupBy(DB::raw("m.ItemRegistrationID"));

我尝试修改'tempoh_negeri'请求选择,它可以正常运行,但仍然无法获得所需的结果。

  if(request('tempoh_negeri')) {
            $query->select(DB::raw('m.itemregistrationpangkatID, m.itemRegistrationID, sum(m.duration)'))
                  ->from(DB::raw('(SELECT itemRegistrationID, itemregistrationpangkatID, NegeriID, yeartamatkhidmat - yearmulakhidmat as duration FROM itemregistrationpangkat) AS m 
                      RIGHT JOIN itemregistrations ON itemregistrations.itemRegistrationID=m.itemRegistrationID'))
                  ->groupBy('m.itemRegistrationID');

结果以json格式发送,以使用ajax显示。由于查询未按预期工作,因此显示错误500。

调试器显示此错误:

   Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'from `itemregistrations` 
   inner join `sections` on `itemregistrations`.`SectionID' at line 1 (SQL: 
   select m.ItemRegistrationID, sum(m.duration) 
   from (SELECT itemregistrationpangkat.itemRegistrationID, itemregistrationpangkat.yeartamatkhidmat - itemregistrationpangkat.yearmulakhidmat as duration 
   FROM itemregistrationpangkat) as m 
   from `itemregistrations` 
   inner join `sections` on `itemregistrations`.`SectionID` = `sections`.`SectionID` 
   inner join `categories` on `itemregistrations`.`CategoryID` = `categories`.`CategoryID` 
   inner join `operasi` on `itemregistrations`.`OperasiID` = `operasi`.`OperasiID`  
   inner join `negeri` on `itemregistrations`.`NegeriID` = `negeri`.`NegeriID` 
   inner join `gred` on `itemregistrations`.`GredID` = `gred`.`GredID` inner join `itemregistrationpangkat` on `itemregistrationpangkat`.`itemRegistrationID` = `itemregistrations`.`itemRegistrationID` 
   where `itemregistrations`.`statusProID` = 1 and `itemregistrations`.`CategoryID` = 1 and `itemregistrations`.`OperasiID` = 9 and `itemregistrationpangkat`.`NegeriID` = 2 group by m.ItemRegistrationID)

结合这些SQL查询的正确语法是什么?

1 个答案:

答案 0 :(得分:1)

我已经使用Laravel SQL来获取每个itemregistrationID的持续时间总和组:

   if(request('tempoh_negeri')) {
            $query->select(DB::raw('m.itemRegistrationID, m.NegeriID, sum(distinct m.duration)'))
                  ->from(DB::raw('(SELECT itemRegistrationID, NegeriID, yeartamatkhidmat - yearmulakhidmat as duration FROM itemregistrationpangkat) AS m 
                        RIGHT JOIN itemregistrations ON itemregistrations.itemRegistrationID=m.itemRegistrationID'))
                  ->groupBy('m.itemRegistrationID') 
                  ->havingRaw('sum(m.duration) >= ?', [request('tempoh_negeri')]);
  }