0

以日期为时间戳的第一笔收入和第二笔支出以及每个月的收入和支出总额,现在我想将其列在一张表格中,如下所示

Month | Income | Expense | Profit

到现在为止,我只能将一个表与总金额分组,但我需要一个可以同时处理两个表的查询。

我的查询是

$result = mysql_query("SELECT 
    date_on,
    MONTHNAME(FROM_UNIXTIME(date_on)) as expmonth,
    YEAR(FROM_UNIXTIME(date_on)) as expyear,
    sum(amount) as exp_total

    from finance_grdps_exp

    group by  expmonth,expyear order by date_on DESC")

    or die(mysql_error());  
    $no=0;   
    $countem = 0;
    while($row = mysql_fetch_array($result))
        {$no++;
            echo' 
            <tr class="gradeX">
                <td>'.
                $row['expmonth'].' '.$row['expyear'].'
                </td>
                <td>'.
                $row['exp_total'].'
                </td>
                <td>'.
                $row['income_total'].' /* not working as if now
                </td>
                <td>'.
                PROFIT.'/* not working as if now
                </td>
                </tr> 
                ';
                }

伙计们 样本日期基数是....

收入表....

姓名| 收入日期(时间戳)| 收入金额

经验表

名称|date_on(时间戳)|金额

谢谢

鲁本感谢您的回复....但我收到错误

...在这两种情况下我都收到错误---每个派生表都必须有自己的别名......

SELECT MONTHNAME(FROM_UNIXTIME(date_on)) as expmonth,
       YEAR(FROM_UNIXTIME(date_on)) as expyear,
       SUM(amount) as exp_total, 
       SUM(income_amount) as inc_total 
FROM(SELECT finance_grdps_exp.date_on AS date_on, 
            finance_grdps_exp.amount AS exp_amount, 
            0 AS inc_amount 
     UNION 
     SELECT finance_grdps_income.income_date AS date_on, 
            0 AS exp_amount, 
            finance_grdps_income.income_amount AS inc_amount) 
GROUP BY expyear, expmonth ORDER BY date_on DESC 

请帮忙

4

1 回答 1

0

假设您有以下结构的两个表 finance_grdps_exp 和 finance_grdps_inc:

日期_on | 数量
  1. 如果在一个表中没有两行在 date_on 中编码的年份和月份相同,则 date_on 仅包含年份和月份,即所有行的其他字段(日期和时间)都相同。

    您可以将它们与 INNER JOIN 构造结合起来:

    SELECT finance_grdps_exp.date_on AS date_on,
           finance_grdps_exp.amount AS exp_amount,
           finance_grdps_inc.amount AS inc_amount
    FROM finance_grdps_exp INNER JOIN finance_grdps_inc
    ON finance_grdps_exp.date_on = finance_grdps_inc.date_on
    

    结果结构是:

    日期_on | exp_amount | inc_amount

    然后让我们将您的想法应用于生成的结构:

    SELECT date_on,
           MONTHNAME(FROM_UNIXTIME(finance_grdps_exp_and_inc.date_on)) as expmonth,
           YEAR(FROM_UNIXTIME(finance_grdps_exp_and_inc.date_on)) as expyear,
           SUM(finance_grdps_exp_and_inc.exp_amount) as exp_total,
           SUM(finance_grdps_exp_and_inc.inc_amount) as inc_total
    
    FROM (SELECT finance_grdps_exp.date_on AS date_on,
                 finance_grdps_exp.amount AS exp_amount,
                 finance_grdps_inc.amount AS inc_amount
    FROM finance_grdps_exp INNER JOIN finance_grdps_inc
    ON finance_grdps_exp.date_on = finance_grdps_inc.date_on) AS finance_grdps_exp_and_inc
    
    GROUP BY expyear, expmonth ORDER BY date_on DESC
    

    mysql_query 给我们:

        [
          expmonth,
          expyear,
          exp_total /* expense per month */,
          inc_total /* income per month */
        ]
    

    为确保 INNER JOIN 语句的最大性能为两个表中的 date_on 字段创建索引。

  2. 其他情况(使用 UNION SELECT)。

    SELECT MONTHNAME(FROM_UNIXTIME(finance_grdps_exp_and_inc.date_on)) as expmonth,
           YEAR(FROM_UNIXTIME(finance_grdps_exp_and_inc.date_on)) as expyear,
           SUM(finance_grdps_exp_and_inc.exp_amount) as exp_total,
           SUM(finance_grdps_exp_and_inc.inc_amount) as inc_total
    FROM(SELECT finance_grdps_exp.date_on AS date_on,
           finance_grdps_exp.amount AS exp_amount,
           0 AS inc_amount 
           FROM finance_grdps_exp
         UNION 
         SELECT finance_grdps_inc.date_on AS date_on,
           0 AS exp_amount,
           finance_grdps_inc.amount AS inc_amount
           finance_grdps_inc) AS finance_grdps_exp_and_inc
    GROUP BY expyear, expmonth ORDER BY date_on DESC
    
于 2012-05-09T15:01:53.273 回答