简体   繁体   English

总和:两个不同的时间查询

[英]Sum:two different time query

I set up my MySQL database with the field 'buy_datetime' and sell_datetime . 我建立了我的MySQL数据库与外地'buy_datetime'sell_datetime I want to get result of total time with different datetime in Hours. 我想获得以小时为单位的不同日期时间的总时间结果。 Format: '1000-01-01 00:00:00' . 格式: '1000-01-01 00:00:00'

For example, 例如,

  1. buy_datetime - sell_datetime = 1result buy_datetime-sell_datetime = 1结果
  2. buy_datetime - sell_datetime = 2result buy_datetime-sell_datetime = 2结果

OUTPUT Total in hours = 1resul + 2result . 输出Total in hours = 1resul + 2result

    public function firstHourHoldingTime(){

        $count_firsthour_holding_time =  DB::table('finaltrade')
            ->select(DB::raw('finaltrade.*'))
            ->sum(TIMEDIFF(Hour,"buy_datetime", "sell_datetime"))
            ->get();
    }

You need raw expression to sum up hourly difference between your columns 您需要原始表达式来总结列之间的每小时差异

public function firstHourHoldingTime(){

    $count_firsthour_holding_time =  DB::table('finaltrade')
        ->select(DB::raw('SUM(TIMESTAMPDIFF(HOUR,buy_datetime, sell_datetime)) as total'))
        ->get();
}

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

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