以日期为时间戳的第一笔收入和第二笔支出以及每个月的收入和支出总额,现在我想将其列在一张表格中,如下所示
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
请帮忙