如何使用WhereBetween(时间和日期分开的列)?

时间:2019-12-10 09:35:44

标签: laravel

我的表格有3列:

date1,time1,value

然后我做一个函数:

function getdata(Request $request)
{
  $start_date = date('d-m-Y 00:00:00');
  $end_date = date('d-m-Y 23:59:59');
  if($request->start_date != '' && $request->end_date != '')
  {
    $dateScope = array($request->start_date, $request->end_date);
  } else {
    $dateScope = array($start_date, $end_date);
  };
  $weather = chaira_weather::selectRaw('(date1+time1) as timestamp,value')
  ->whereBetween('timestamp', $dateScope)
  ->orderBy('timestamp', 'ASC')
  ->get();

  return response()->json($weather);

}

我有一个错误:

Undefined column: 7 ERROR:  column "timestamp" does not exist

有什么解决方法吗?

编辑2:

CONCAT函数给我错误的日期/时间结果:10/12/201900:00:00。我的方式(date1 + time1)作为时间戳有效。 haveBetween子句不再起作用。但是,如果我仅使用orderby的别名时间戳有效。

我以另一种方式工作:

  $weather = chaira_weather::selectRaw('(date1+time1) as timestamp,value')
  ->whereBetween('date1', $dateScope)
  ->whereBetween('time1', $dateScope)
  ->orderBy('timestamp', 'ASC')
  ->get();

现在可以使用了。但是又有问题。当time1具有相同的值时。该查询每天只给我一条记录。例如,如果日期和时间在01/12/2010 01:00:00和10/12/2010 01:00:00之间。结果仅在该小时01:00:00:

01/12/2010 01:00:00,1
02/12/2010 01:00:00,3
03/12/2010 01:00:00,56
04/12/2010 01:00:00,7
05/12/2010 01:00:00,6
06/12/2010 01:00:00,8
07/12/2010 01:00:00,6
08/12/2010 01:00:00,7
09/12/2010 01:00:00,6
10/12/2010 01:00:00,32

如果查询中的时间不同,则效果很好。有想法吗?

5 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

     $weather = DB::table('chaira_weather')
                    ->select('CONCAT(date1, time1) as timestamp, value')
                    ->whereBetween('timestamp', $dateScope)
                    ->orderBy('timestamp', 'ASC')
                    ->get(); 

答案 1 :(得分:0)

使用1.3

select('CONCAT(date1, time1)

答案 2 :(得分:0)

也尝试这个。

$weather = chaira_weather::select('CONCAT_WS(" ",date1,time1) as timestamp, value')
                ->whereBetween('timestamp', $dateScope)
                ->orderBy('timestamp', 'ASC')
                ->get(); 

编辑1:

$weather = chaira_weather::select('STR_TO_DATE(CONCAT_WS(" ",date1,time1),"%d,%m,%Y %H:%i:%s") as timestamp, value')
                ->whereBetween('timestamp', $dateScope)
                ->orderBy('timestamp', 'ASC')
                ->get(); 

编辑2:

$weather = chaira_weather::select('value')
                ->whereRaw('STR_TO_DATE(CONCAT_WS(" ",date1,time1),"%d,%m,%Y %H:%i:%s") >= ? AND STR_TO_DATE(CONCAT_WS(" ",date1,time1),"%d,%m,%Y %H:%i:%s") <= ?',array($startDate, $endDate))
                ->orderBy('timestamp', 'ASC')
                ->get(); 

只需在比较时检查格式即可。

答案 3 :(得分:0)

您不能在别名列上使用where子句。您可以grouporder并仅在别名列上使用having。因此请使用having而不是where

$weather = chaira_weather::selectRaw('CONCAT(date1,time1) as timestamp, value')
                ->havingBetween('timestamp', $dateScope)
                ->orderBy('timestamp', 'ASC')
                ->get();

答案 4 :(得分:0)

未经测试,但我认为您可以创建chaira_weather模型,您可以定义范围并传递变量(日期)以过滤出结果

public function scopeStartDate($query, $start){
    return $query->where('start', '>=',Carbon::parse($start, 'Y-m-d'));
}

public function scopeEndDate($query, $end){
    return $query->where('end', '<=', Carbon::parse($end, 'Y-m-d'));
}

然后您可以使用like

chaira_weather::startDate($startDate)->endDate($endDate)->get();