exchanges
表格结构:
id exchange created_at updated_at deleted_at start_time close_time
---- ---------- --------------------- --------------------- ------------ ------------ ------------
1 NSE 2018-04-18 06:22:11 2018-04-18 06:22:11 (NULL) 09:15:00 03:30:00
2 BSE 2018-05-06 07:07:53 2018-05-06 07:07:53 (NULL) 00:00:00 00:00:00
3 NYSE 2018-05-19 08:34:07 2018-05-19 08:34:07 (NULL) 00:00:00 00:00:00
finaltrades
表格结构:
id user_id exchange_id market_id symbol_id buy_datetime sell_datetime buy_rate sell_rate quantities
---- --------- ------------- ----------- ----------- --------------------- --------------------- ---------- ----------- ------------
1 1 1 1 96 2018-05-25 18:13:26 0000-00-00 00:00:00 2205 0 100
2 1 1 1 96 0000-00-00 00:00:00 2018-05-25 18:13:59 0 6680 100
3 4 1 1 23 2018-05-25 18:16:27 0000-00-00 00:00:00 0 0 10
4 1 1 1 96 2018-05-25 18:13:59 0000-00-00 00:00:00 50351 0 30
5 1 1 1 15 0000-00-00 00:00:00 2018-05-25 18:34:46 0 100 150
6 4 1 1 573 2018-05-26 09:29:17 2018-05-27 03:10:09 10 10 10
7 1 1 1 15 2018-05-11 09:30:54 2018-05-25 18:34:56 40 100 40
我想减去buy_datetime - sell_datetime = hours
,将该计算应用于所有行并将所有结果添加到一起。
例如,
buy_datetime - sell_datetime = 12 hours
buy_datetime - sell_datetime = 19 hours
buy_datetime - sell_datetime = 11 hours
现在
total all = 32 hours
这是我到目前为止所提出的
public function total_hours_count {
$first_hour_trade = DB::table('finaltrade')
->select('finaltrade.*')
->where('buy_datetime','-','sell_datetime')
->sum() -> get();
return response() -> json($first_hour_trade);
}
我不知道代码中的错误在哪里。
答案 0 :(得分:1)
如果你想从整个表中总结buy_datetime和sell_datetime的差异,你可以使用原始表达式
DB::table('finaltrade')
->select(DB::raw("SUM(TIME_TO_SEC(TIMEDIFF(buy_datetime, sell_datetime )) / 3600) AS total_hours")
->get();