为什么我的复杂' mysql'查询始终返回查询结果' count'为0?

时间:2017-07-07 18:31:24

标签: mysql search filter laravel-5.4 laravel-query-builder

我正在为过滤搜索编写代码,其中用户从3个复选框的列表中选择至少一个选项,然后我必须为所选选项(复选框)编写查询。如果我尝试运行我的代码,那么我得到"查询结果计数等于零,这意味着$ count = 0"。以前,当我第一次编写代码时,它工作正常,但现在它无效。

我的表格是:

<form id="my_form_id" method="post" action="{{ action('MyController@store') }}"">
<div class="form-group">
  <label><input type="checkbox" value="suppliers" name="suppliers">suppliers</label>
</div>
<div class="form-group">
  <label><input type="checkbox" value="campaign" name="campaign">campaign</label>
</div>
<div class="form-group">
  <label><input type="checkbox" value="clients" name="clients">clients</label>
</div>
<div class="form-group">
  <label for="start_date">From:</label><input type="text" name="startdate" id="startdate">
</div>
<div class="form-group">
  <label for="start_date">From:</label><input type="text" name="enddate" id="enddate">
</div>
<div class="form-group">
  <button type="submit" id="process" class="btn btn-primary submit">Run Report</button>
</div>

我的MySql表格结构&amp;数据是:lead_audit

结构:

ColumnName  DataType  Length
  id         int      11
supplier_id  int      11
source       varchar  255
client_id    int      11
campaign_id  varchar  255
data         text 
received     datetime
errors       blob
disposition  varchar  255

id  supplier_id  source  client_id  campaign_id  data  received       errors  disposition
1   6            6          1       56           ---   2017-07-07 11:19:25  False  Accepted
1   6            6          1       56           ---   2017-07-07 11:19:25  False  Accepted

我的查询如下:

if(isset($request->suppliers) && $request->suppliers == 'suppliers'){
            $results->leftjoin('suppliers AS s', 's.id', '=', 'l.supplier_id')
                    ->addSelect('s.name AS Supplier')
                    ->groupBy('s.name')
                    ->orderBy('s.name');
}
if(isset($request->source) && $request->source == 'source'){
            $results->addSelect('l.source AS Source')
                    ->groupBy('l.source')
                    ->orderBy('l.source');
            //$results->leftjoin('source AS so', 'so.id', '=', 'l.source_id')
}
if(isset($request->campaign) && $request->campaign == 'campaign'){
            $results->leftjoin('campaigns AS c', 'c.id', '=', 'l.campaign_id')
                    ->addSelect('c.name AS Campaign')
                    ->groupBy('c.name')
                    ->orderBy('c.name');
}    
if(isset($request->clients) && $request->clients == 'clients'){
            $results->leftjoin('clients AS cl', 'cl.id', '=', 'l.client_id')
                    ->addSelect('cl.name AS Client')
                    ->groupBy('cl.name')
                    ->orderBy('cl.name');
} 
$queryResults = $results->addSelect('l.disposition AS Disposition')
                ->selectRaw('COUNT(*) as Count')
                ->whereBetween('l.received', [$start, $end])
                ->groupBy('l.disposition')
                ->orderBy('l.disposition')
                ->get()->toArray();
$count = count($queryResults);
echo "Count: ".$count;exit;

if($count > 0){
  $results = array_map(function($item) {
            return (array)$item;
        }, $queryResults);
  return Excel::create('my_excel_name', function($excel) use ($results) {
            $excel->sheet('sheet_name', function($sheet) use ($results) {
                $sheet->fromArray($results);
            });
        })->setFilename($filename)
        ->download('csv');
} else{
        \Session::flash('error','No data has been found.');
        return redirect('my_controller/create');
    }

结果查询是:

array:1 [▼
0 => array:3 [▼
"query" => "select `s`.`name` as `Supplier`, `l`.`source` as `Source`, `c`.`name` as `Campaign`, `cl`.`name` as `Client`, `l`.`disposition` as `Disposition`, COUNT(*) as Count from `lead_audit` as `l` left join `suppliers` as `s` on `s`.`id` = `l`.`supplier_id` left join `campaigns` as `c` on `c`.`id` = `l`.`campaign_id` left join `clients` as `cl` on `cl`.`id` = `l`.`client_id` where `l`.`received` between ? and ? group by `s`.`name`, `l`.`source`, `c`.`name`, `cl`.`name`, `l`.`disposition` order by `s`.`name` asc, `l`.`source` asc, `c`.`name` asc, `cl`.`name` asc, `l`.`disposition` asc ◀"
"bindings" => array:2 [▼
  0 => "2017-07-01"
  1 => "2017-07-08"
]
"time" => 149.01
]
]

If I print below:
print_r($queryResults);
echo "Count: ".$count;exit;

then o/p
  Array ( ) Count: 0

为什么我总是得到$ count值&#39; 0&#39;。我的代码有什么问题?

1 个答案:

答案 0 :(得分:0)

发布答案为时已晚,但我希望这可能有助于某人。我犯的错误在于给出如下的开始日期和结束日期格式:

问题:

$start = "2017-07-01"
$end = "2017-07-08"

但实际上我必须提供如下输入:

解决方案:

$start = "2017-07-01 00:00:00"
$end = "2017-07-08 00:00:00"

在开始和结束日期添加时间为“00:00:00”后,我的问题得以解决。谢谢大家。