douyin2962 2012-04-21 18:13
浏览 34
已采纳

MySql / PHP SUM 2表1查询

I have been having the most difficult time ever with this problem. I have 2 tables with total columns that I want to SUM together. They both have the same columns, I am using two tables as one is a script generated table of data and the other is user entered data and we need them separate. Except now we need to SUM(total) them together.

Table 1

   +-----------+-----+--------+------+
   | date      |t_id | t_port | total|
   +-----------+-----+--------+------+
   |2012-04-01 | 1271| 101    | 80.00|
   +-----------+-----+--------+------+

Table 2

+----------+------+--------+-------+
| date     | t_id | t_port | total |
+----------+------+--------+-------+
|2012-04-20| 1271 | 101    | 120.00|
+----------+------+--------+-------+

Total should be $200.00

HERE IS MY QUERY

"SELECT SUM(cntTotal) as total FROM CBS_WO WHERE (date BETWEEN '$monthSecond' AND '$monthEnd') AND t_port = '$t_port' AND t_id = '$t_id' UNION SELECT SUM(cntTotal) as total FROM CNT_MODS WHERE (date BETWEEN '$monthSecond' AND '$monthEnd') AND t_port = '$t_port' AND t_id = '$t_id'"

This query seems to work in phpMyAdmin as I get 2 rows. (1 for each table), logically I used a WHILE loop in PHP to add the two rows together. After echo'ing out each row manually I discovered my second row isn't showing up in the loop, yet it does in the query?

Can't figure out why this is happening, I am certain it's something silly but I been at this code for over 16hrs already and need a new set of eyes.

PHP CODE

function periodTotal()
{
    include('/sql.login.php');  

    $t_id                   = "1271";
    $t_port                 = "101";
    $date                   = date("Y-m-d");

    # FIND MONTH (DATE)
    $monthStart     = date("Y-m-d", strtotime(date('m').'/01/'.date('Y').' 00:00:00')); 
    $monthFirst     = date("Y-m-d", strtotime('-1 second',strtotime('+15 days',strtotime(date('m').'/01/'.date('Y').' 00:00:00'))));
    $monthSecond    = date("Y-m-d", strtotime('-1 second',strtotime('+16 days',strtotime(date('m').'/01/'.date('Y').' 00:00:00'))));
    $monthEnd       = date("Y-m-d", strtotime('-1 second',strtotime('+1 month',strtotime(date('m').'/01/'.date('Y').' 00:00:00'))));

    if ($date = $monthFirst) 
    {
        $sql = $dbh->prepare("SELECT SUM(cntTotal) as total FROM CBS_WO WHERE (date BETWEEN '$monthStart' AND '$monthFirst') AND t_port = '$t_port' AND t_id = '$t_id' UNION SELECT SUM(cntTotal) as total FROM CNT_MODS WHERE (date BETWEEN '$monthStart' AND '$monthFirst') AND t_port = '$t_port' AND t_id = '$t_id'");
        $sql->execute();
    } 
    else 
    {
        $sql = $dbh->prepare("SELECT SUM(cntTotal) as total FROM CBS_WO WHERE (date BETWEEN '$monthSecond' AND '$monthEnd') AND t_port = '$t_port' AND t_id = '$t_id' UNION SELECT SUM(cntTotal) as total FROM CNT_MODS WHERE (date BETWEEN '$monthSecond' AND '$monthEnd') AND t_port = '$t_port' AND t_id = '$t_id'");
        $sql->execute();
    }
        while($row = $sql->fetch(PDO::FETCH_ASSOC))
        {
            $total += $row['total'];
        }

    return $total;
}
  • 写回答

2条回答 默认 最新

  • dongluzhi5208 2012-04-21 18:17
    关注

    Does this work for you?

    SELECT SUM(`total`) as `total` FROM ((
        SELECT SUM(cntTotal) as total FROM CBS_WO
        WHERE (date BETWEEN '$monthSecond' AND '$monthEnd') AND t_port = '$t_port' AND t_id = '$t_id'
    ) UNION (
        SELECT SUM(cntTotal) as total FROM CNT_MODS
        WHERE (date BETWEEN '$monthSecond' AND '$monthEnd') AND t_port = '$t_port' AND t_id = '$t_id'"
    )) as temp
    

    This might be more efficient:

    SELECT SUM(total) FROM (
        SELECT cntTotal FROM CBS_WO
        WHERE (date BETWEEN '$monthSecond' AND '$monthEnd') AND t_port = '$t_port' AND t_id = '$t_id'
    ) UNION (
        SELECT cntTotal FROM CNT_MODS
        WHERE (date BETWEEN '$monthSecond' AND '$monthEnd') AND t_port = '$t_port' AND t_id = '$t_id'"
    ) as temp
    

    (only has one SUM) but you'd have to test it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真