如何在laravel 4.2中使用mysql中的计数值查询

时间:2015-07-20 05:06:56

标签: php mysql laravel laravel-4

我希望检查laravel 4.2 query

的where条件中的s​​um值和count值

这是我的代码

    $aColumns = array('driver_details.DriverId', DB::raw('CONCAT(driver_details.Firstname, " ", driver_details.Lastname, " / ", driver_details.TaxiPlateNo) AS TaxiDriver'), DB::raw('count(taxi_trip.AutoId) AS TotalTrip'), DB::raw('sum(taxi_trip.TripDistance) AS TotalTripDistance'), DB::raw('sum(taxi_trip.TotalFare) AS TotalTripFare'));       
    $aColumnsSearch = array('driver_details.DriverId', 'driver_details.Firstname', 'driver_details.Lastname','driver_details.TaxiPlateNo', 
    DB::raw('count(taxi_trip.AutoId)'));

$Search='';
if(Input::get('Search')!='')
    $Search = Input::get('Search');     
$DriverId='';
if(Input::get('DriverId')!='')
    $DriverId = Input::get('DriverId');

$IsCancel='';
if(Input::get('IsCancel')!='')
    $IsCancel = Input::get('IsCancel');

$FromDate='';
if(Input::get('FromDate')!='')
    $FromDate = Input::get('FromDate');

    $ToDate='';
    if(Input::get('ToDate')!='')
        $ToDate = Input::get('ToDate');

$tempqry = DB::connection()->table('driver_details')    
    ->select($aColumns)
    ->leftJoin('taxi_trip', function($join)
          {
             $join->on('taxi_trip.DriverId', '=', 'driver_details.DriverId');
             $join->on('taxi_trip.PickupLAT', '!=', DB::raw(0));
             $join->on('taxi_trip.DropLAT', '!=', DB::raw(0));
             $join->on('taxi_trip.TotalFare', '!=', DB::raw(0));
           })
        ->where(function($query) use ($FromDate) {
       if($FromDate!='')
        {
            $query->where(DB::raw("date_format(taxi_trip.RequestDate,'%Y-%m-%d')"), '>=', date("Y-m-d",strtotime($FromDate)));
        }
    })
    ->where(function($query) use ($ToDate) {
       if($ToDate!='')
        {
            $query->where(DB::raw("date_format(taxi_trip.RequestDate,'%Y-%m-%d')"), '<=', date("Y-m-d",strtotime($ToDate)));
        }
    })
    ->where(function($query) use ($Search, $aColumnsSearch) 
            {
                for ( $i=0 ; $i<count($aColumnsSearch) ; $i++ )
                {
                    //$query->orWhere($aColumnsSearch[$i].' LIKE %'.$Search.'%');   

                    $query->orWhere($aColumnsSearch[$i],'LIKE', '%'.$Search.'%');
                }
            })
        ->where(function($query) use ($DriverId) {
       if($DriverId!='')
        {
            $query->where('taxi_trip.DriverId', '=', $DriverId);
        }
    })

    ->groupby('driver_details.DriverId');

    $temp = $tempqry;
    $tempqry  = $temp->get();

    return $tempqry;

我希望使用从查询得到的count()和sum()值与我给出的搜索值

2 个答案:

答案 0 :(得分:0)

你试过这样的吗?

public function myfunction($query, $tag)
{
    return $query->where('title', 'LIKE', '%'.$tag.'%');
}

MyTable::where('name', 'LIKE', '%'.$name.'%')->get();

MyTable::like('name', 'Praveen')->get();

答案 1 :(得分:0)

我不知道您所遵循的表结构,但这是您期望的工作逻辑。

第1步:

获取用户的输入并获取与您的计数相匹配的记录列表,如下所示。

$inputCount = '100'; #Assign the user input here
$result = DB::table('users')
                    ->orderBy('name', 'desc')
                    ->groupBy('count')
                    ->having('count', '>', $inputCount)
                    ->get();

现在,您将拥有用户列表,其总计数与用户的输入相匹配。

注意:count与总计数

不同

第2步:

只需使用foreach

进行迭代
@foreach ($result as $data)
    <p>This is user {{ $data->id }}</p>
@endforeach

注意:如果您使用数据表,那么您应该使用表结构进行迭代。

附加要点:

如果您需要检索迭代内部的用户信息。

您将获得迭代内的迭代实体细节

即,摘取用户的数据

$name = DB::table('users')->where('id', $result->id)->pluck('name');

所以,你应该

@foreach ($result as $data)
<p>This is user {{ $data->id }}</p>
$name = DB::table('users')->where('id', $data->id)->pluck('name');
@endforeach

推荐:

我建议您使用eloquent