MySQL结合了3个支付表

时间:2016-09-14 16:54:04

标签: php mysql

我的管理面板上有一个折线图,我希望用它来显示按年度季度分组的付款信息,从3个不同的表格中选择3个不同的项目。

我目前正在分别收集所有结果:

$quarter_invoice = array();
$quarter_ads = array();
$quarter_account = array();

 $get_invoice = "SELECT SUM(txn_amount) as total, QUARTER(txn_date) as qt, YEAR(txn_date) as year
FROM ap_invoice_payments
GROUP BY year,qt";

if ($result = mysqli_query($conn, $get_invoice)) {

    /* fetch associative array */
    while ($row = mysqli_fetch_assoc($result)) {

        $quarter_invoice[] = "{y: '".$row["year"]." Q".$row["qt"]."', item1: ".$row["total"]."},";
    }
}

 $get_ads = "SELECT SUM(transfer_amount) as total, QUARTER(transfer_date) as qt, YEAR(transfer_date) as year
FROM ap_acc_transfers WHERE `reference` LIKE '%Ad Campaign Purchase%' 
GROUP BY year,qt";

if ($result = mysqli_query($conn, $get_ads)) {

    /* fetch associative array */
    while ($row = mysqli_fetch_assoc($result)) {

        $quarter_ads[] = "{y: '".$row["year"]." Q".$row["qt"]."', item2: ".$row["total"]."},";
    }
}

 $get_account = "SELECT SUM(snappysites_fee) as total, QUARTER(txn_date) as qt, YEAR(txn_date) as year
FROM ap_acc_payments 
GROUP BY year,qt";

if ($result = mysqli_query($conn, $get_account)) {

    /* fetch associative array */
    while ($row = mysqli_fetch_assoc($result)) {

        $quarter_account[] = "{y: '".$row["year"]." Q".$row["qt"]."', item3: ".$row["total"]."},";
    }
}

当我输出这段代码时,一切正常并且显示如下:

Array ( [0] => {y: '2016 Q1', item1: 692.00}, [1] => {y: '2016 Q2', item1: 809.00}, [2] => {y: '2016 Q3', item1: 290.00}, ) 
Array ( [0] => {y: '2016 Q3', item2: 8.00}, ) 
Array ( [0] => {y: '2016 Q2', item3: 1.00}, [1] => {y: '2016 Q3', item3: 2.50}, )

虽然我想通过组合所有3个查询来实现这种结果:

{y: '2016 Q1', item1: 692.00, item2: 0, item3: 0},
{y: '2016 Q2', item1: 809.00, item2: 0, item3: 1.00},
{y: '2016 Q3', item1: 290.00, item2: 8.00, item3: 2.50},

现在我稍微了解了如何连接表,尽管这些表中没有一个共享相同的列信息,而且对于其中一些结果,如上所示,显示NULL。在我的第一次尝试中,而不是显示0如果NULL,你根本没有得到任何结果,我确信这可能会导致一些问题,不是吗?

如果有人能指出我正确的方向来获得这些类型的结果,那将非常有帮助。

0 个答案:

没有答案