如何获取不同表中的其他列值到查询中?

时间:2019-04-26 02:43:03

标签: sql laravel-5 subquery

我已经搜索了应用程序,找到了已被某些条件(类别,经验年限等)过滤的个人信息

我对最后一个过滤器“ negeri的tempoh perkhidmatan”有疑问。我需要按州(negeri)计算工作经验的数量。例如,当搜索州(negeri)“ x”中的5年人员时,sql将汇总所选州每个人的经验年限。

这是按条件搜索SQL的完整代码:

   $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_lahir') != ''){
            $query->where('itemregistrations.NegeriID', $request->input('negeri_lahir'));
        }

        if($request->input('kategori') != '') {
            $query->where('itemregistrations.CategoryID', $request->input('kategori'));
        }

        if($request->input('pangkat') != '') {
            $query->where('itemregistrations.OperasiID', $request->input('pangkat'));
        }

        if(request('umur')) {
            $query->whereRaw('YEAR(CURDATE()) - lahir_yy >= ?', [request('umur')]);  
        }

        if($request->input('gred') != '') {
            $query->where('itemregistrations.GredID', $request->input('gred'));
        }

        if(request('tempoh')) {
            $query->whereRaw('YEAR(CURDATE()) - pdrm_yy >= ?', [request('tempoh')]);  
        }

        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(DB::raw('(SELECT itemRegistrationID, NegeriID, yeartamatkhidmat - yearmulakhidmat as duration FROM itemregistrationpangkat) AS m 
                        RIGHT JOIN itemregistrations ON itemregistrations.itemRegistrationID=m.itemRegistrationID'))
                  ->distinct()
                  ->groupBy('m.itemRegistrationID');
      }

   $newitem = $query->get();

   return response::json($newitem);

涉及解决的代码是这个(最后一个过滤器):

    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', 'm.NegeriID'); 
      }

问题是我需要从$ query语句的itemregistrations表中获取名称列,sectionID列,CategoryID,OperasiID,NegeriID,GredID。如何将“ tempoh_negeri”中的最后一个查询过滤器与上一个查询过滤器相结合?

1 个答案:

答案 0 :(得分:2)

我并不特别了解Laravel,所以在尝试理解查询的构建方式时遇到了很多麻烦,但是这种语法似乎使人们可以通过添加块(但不一定以正确的顺序)来编写请求。因此,这是我认为您的查询应该针对SQL说话者执行的操作:

SELECT itemregistrations .name,
       sections          .sectionname,
       categories        .categoryname,
       operasi           .operasiname,
       itemregistrations .Nobadan,
       itemregistrations .lahir_yy,
       itemregistrations .pdrm_yy,
       gred              .namagred,
       itemregistrations .itemRegistrationID
-- if($tempoh_negeri)           (request)
      ,m                 .itemRegistrationID,
       sum(m.duration)

  FROM itemregistrations
-- if($tempoh_negeri)           (request)
     ,(SELECT DISTINCT itemRegistrationID,
                       NegeriID,
                       yeartamatkhidmat - yearmulakhidmat as duration

         FROM itemregistrationpangkat) AS m

   RIGHT JOIN itemregistrations
           ON itemregistrations.itemRegistrationID = m.itemRegistrationID


  JOIN sections
    ON itemregistrations.SectionID = sections.SectionID

  JOIN categories
    ON itemregistrations.CategoryID = categories.CategoryID

  JOIN operasi
    ON itemregistrations.OperasiID  = operasi.OperasiID

  JOIN negeri
    ON itemregistrations.NegeriID   = negeri.NegeriID

  JOIN gred
    ON itemregistrations.GredID     = gred.GredID

-- if($negeri_perkhidmatan)
  JOIN itemregistrationpangkat
    ON itemregistrationpangkat.itemRegistrationID = itemregistrations.itemRegistrationID


 WHERE itemregistrations.statusProID = 1

-- if($negeri_lahir) (WHERE)
   AND itemregistrations.NegeriID           = $negeri_lahir

-- if($kategori)     (WHERE)
   AND  itemregistrations.CategoryID        = $kategori

-- if($pangkat)      (WHERE)
   AND itemregistrations.OperasiID          = $pangkat

-- if(umur)          (WHERERAW) (request)
   AND YEAR(CURDATE()) - lahir_yy          >= umur

-- if($gred)         (WHERE)
   AND itemregistrations.GredID             = $gred

-- if($tempoh)       (WHERERAW) (request)
   AND YEAR(CURDATE()) - pdrm_yy           >= tempoh

-- if($negeri_perkhidmatan)
   AND itemregistrationpangkat.NegeriID     = $negeri_perkhidmatan

-- if($tempoh_negeri)           (request)
GROUP BY m.itemRegistrationID

如果是这样,您将无法执行该操作(包括子查询中的主列),因为您的子查询将在主查询之前进行评估。

相反,您需要在主查询级别编写一个适当的过滤器,即::已经存在的其他“ JOIN…ON”子句。会给出:

LEFT JOIN itemregistrationpangkat
       ON itemregistrationpangkat.itemRegistrationID = itemregistrations.itemRegistrationID

…然后直接在您的sum()函数中指定减法

          sum(yeartamatkhidmat - yearmulakhidma)

关于Lavarel,这可能会给出类似的信息:

 if(request('tempoh_negeri')) {
    $query->leftjoin('itemregistrationpangkat','itemRegistrationID','=','itemregistrations.itemRegistrationID');
    $query->select(DB:raw('sum(yeartamatkhidmat - yearmulakhidmat'));
 }