両方のタイムスタンプとして日付を含む最初の収入と2番目の支出、および各月の収入と支出の合計、次のようにテーブルに入れたい
Month | Income | Expense | Profit
これまで、金額を合計として 1 つのテーブルしかグループ化できませんでしたが、両方のテーブルを同時に処理できるクエリが必要です。
私のクエリは
$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>
';
}
Guys サンプルのデータベースは ....
収入表……
名前| 収入_日付 (タイムスタンプ) | 収入_金額
指数表
名前|日時 (タイムスタンプ) |金額
どうも
Ruben 返信ありがとうございます ....しかし、エラーが発生しています
...どちらの場合もエラーが発生します---すべての派生テーブルには独自のエイリアスが必要です....
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
助けてください