简体   繁体   English

SQL:查询使用计数返回1

[英]SQL: Query returns 1 using count

I have a query in PHP(MySQL). 我在PHP(MySQL)中有一个查询。 I am fetching records from a table and returning them to a datatable. 我正在从表中获取记录,并将它们返回到数据表中。

The problem is it always returns 1 when I use count() with the query. 问题是当我在查询中使用count()时,它总是返回1。

However, if I count the elements of the array, then the result is as expected. 但是,如果我计算数组的元素,那么结果将与预期的一样。 It returns all 8 records. 它返回所有8条记录。

Following is my code: 以下是我的代码:

public function job_order_detail_del($arr=array()) {
        $count = 0;
        $start = isset($arr['start'])?$arr['start']:0;
        $length = isset($arr['length'])?$arr['length']:0;
        $search = isset($arr['search'])?$arr['search']:'';
        $orderBy = isset($arr['orderBy'])?$arr['orderBy']:'';
        $orderDir = isset($arr['orderDir'])?$arr['orderDir']:'';
        $aufnr = isset($arr['aufnr'])?$arr['aufnr']:0;
        $aufpl = isset($arr['aufpl'])?$arr['aufpl']:0;
        $type = isset($arr['type'])?$arr['type']:'';
        $whr = array('h.stats' => '', 'h.bukrs' => Session::get('company'), 'h.delstats' => '');
        if ($aufnr > 0)
            $whr['a.aufnr'] = $aufnr;
        if ($aufpl > 0)
            $whr['a.aufpl'] = $aufpl;

        $a = DB::table('zpp_afpo_h as h')
                ->leftjoin('zpp_afpo as a ', function($join) {
                    $join->on('a.aufnr', '=', 'h.aufnr')
                    ->where('a.bukrs', '=', Session::get('company'))
                    ->where('a.stats', '=', '');
                })
                ->leftjoin('zpp_afvc as b ', function($join) {
                    $join->on('a.aufnr', '=', 'b.aufnr')
                    ->on('a.aufpl', '=', 'b.aufpl')
                    ->where('b.bukrs', '=', Session::get('company'))
                    ->where('b.stats', '=', '');
                })
                ->leftjoin('zpp_afru as c ', function($join) use($type) {
                    $join->on('c.aufnr', '=', 'b.aufnr')
                    ->on('c.rueck', '=', 'b.rueck');
                })
                ->where('c.type', '=', $type)
                    ->where('c.bukrs', '=', Session::get('company'))
                    ->where('c.stats', '=', '')
                    ->where('c.delstats', '=', '')
                ->select('h.idat2', 'a.matnr', 'b.arbpl', 'a.gamng as total', 'b.rueck', 'h.priority', 'b.vornr', 'b.prev_startper', 'b.scrap', 'h.dispatch_date', 'h.order_start_dt', 'h.requirement_dt', 'h.ktxt', 'c.rmzhl', 'c.budat', 'c.aufnr', 'c.rework_lmnga', DB::raw('sum(c.lmnga) as sum_cnfrm'));
        if ($aufnr == '') {
            $a->where('h.idat2', '=', '0000:00:00');
        }
        $a->where($whr)
                ->groupby('b.rueck')
                ->groupby('c.rmzhl')
                ->groupby('c.counter');
        if($orderBy!=''){
                $a->orderBy($orderBy,$orderDir);
        }
        if($search!=''){
          $dt_srch = implode('-',array_reverse(explode('-',$search)));
          $a->where(function($query) use ($search,$dt_srch){
            $query->where('c.aufnr','LIKE','%'.$search.'%')
                  ->orWhere('a.matnr','LIKE','%'.$search.'%')
                  ->orWhere('c.budat','LIKE','%'.$dt_srch.'%')
                  ->orWhere('b.arbpl','LIKE','%'.$search.'%')
                  ->orWhere('a.gamng','LIKE','%'.$search.'%');
          });
        }
        if($length>0){
          $get_rec = $a->skip($start)->take($length)->get();
        }else{
          $get_rec = $a->get();
          $count = count($get_rec);
//          $count = $a->count(); //Problem is here
          return $count;
        }
        $arr = array();
        foreach($get_rec as $l){
                    if($length>0 || Input::get('open') == 1 || Input::get('open') == 2){
                        $arr = DB::table('maras')
                                    ->where('matnr','=',$l->matnr)
                                    ->where('bukrs','=',Session::get('company'))
                                    ->where('stats','=','')
                                    ->where('delstats','=','')
                                    ->select(DB::raw('GROUP_CONCAT(distinct(matnr)) as mat'),DB::raw('GROUP_CONCAT(distinct(mdesc)) as mat_desc'))
                                    ->groupby('matnr')
                                    ->first();
                        $l->matnr = $arr->mat;
                        $l->mat_desc = $arr->mat_desc;
                    }

                }

        return $get_rec;
    }

Please let me know the problem. 请让我知道问题所在。 Thanks in advance. 提前致谢。

You are using count on groupBy element and count is returning only the grouped count. 您在groupBy元素上使用count ,而count仅返回grouped计数。 That's why it is returning only single row or 1. You could count the returned collections in this case like- 这就是为什么它仅返回single或1。在这种情况下,您可以count返回的集合,例如-

$count = $get_rec->count();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM