获取关联分组结果

时间:2013-07-06 19:14:40

标签: php mysql sql

我正在使用以下SQL查询从我的“工作表数据”表中提取数据。使用此查询,将返回以下数据...

id         date         sportsbook         bet         to_win         status
1          2013-07-05   bu                 1000.00     1200.00        1
2          2013-07-05   bm                 1500.00     238.94         2
3          2013-07-05   bm                 100.00      55.00          1
4          2013-07-06   bm                 2000.00     1850.57        1
5          2013-07-06   5d                 280.00      127.85         2

我已经安装了PHP,它将值加在一起,为赌注提供“运行总计”。根据状态,它将bet或to_win中的值适当地添加到数组中,然后我稍后使用数组的总和。举个小例子,见下文......

while ($row = $results->fetch_assoc()) {

    if($row["status"] === '1') {
        $total[] = $row["to_win"];
    }

    if($row["status"] === '2') {
        $total[] = '-' . $row["bet"];
    }

}

echo 'Running Total (Lifetime): ' . array_sum($total);

基本上我要做的就是使用上面相同的逻辑,我想分别获得每个日期和每个体育博彩的运行总计。我想将其插入到数据库表中供以后参考,但INSERT查询很简单。最后,我有我的问题......

  1. 按日期和方式将数据组合在一起的最佳方式是什么?使用上面的PHP的体育博彩?我知道我可以做这样的事......

     if($row["sportsbook"] == 'bu') {
    
      if($row["status"] === '1') {
          $total[] = $row["to_win"];
      }
    
      if($row["status"] === '2') {
          $total[] = '-' . $row["bet"];
      }
    
     }
    
  2. ...虽然这不会解决日期位(我必须为日期添加另一个条款,这看起来很笨重)。

0 个答案:

没有答案
相关问题