MySql / PHP SUM 2表1查询

时间:2012-04-21 18:13:56

标签: php mysql sum

我遇到过这个问题最艰难的时刻。我有2个表,总列数,我想要一起SUM。它们都有相同的列,我使用两个表,因为一个是脚本生成的数据表,另一个是用户输入的数据,我们需要它们分开。除了现在我们需要将它们(总计)合在一起。

表1

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

表2

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

总计应为$ 200.00

这是我的查询

"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'"

这个查询似乎在phpMyAdmin中工作,因为我得到2行。 (每个表1个),逻辑上我在PHP中使用WHILE循环将两行添加到一起。在手动回显出每一行之后,我发现我的第二行没有显示在循环中,但它在查询中是什么?

无法弄清楚为什么会发生这种情况,我确信这是愚蠢的事情,但我已经在这个代码已经超过16小时了,需要一套新的眼睛。

PHP代码

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 个答案:

答案 0 :(得分:2)

这对你有用吗?

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

这可能更有效:

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

(只有一个SUM),但您必须对其进行测试。

答案 1 :(得分:0)

It seems to be able to display your it in PHP......

=>   set the PDO error mode to exception

     $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $dbh->prepare(Run Sql Query here...........);

=>   exceute the query 

stmt->execute();

=>by setting the FETCH mode we can set

$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);

=> The iterator will fetch the results for us.

 foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) 
        { 
        echo $v; 
        }