3
MySQLデータベースを使用して、毎年の月次データを取得しようとしています。 私は2つのテーブル、どのようにMySQLの右結合の結果を並べ替えるには?
- ローン
- 月
ローン
` +--------+--------+-------+----------+
| idloan | amount | tot | interest |
+--------+--------+-------+----------+
| 3 | 10000 | 15000 | 50 |
| 4 | 5000 | 6000 | 10 |
| 5 | 20000 | 30000 | 10 |
| 6 | 30000 | 3000 | 10 |
| 7 | 15000 | 16500 | 10 |
+--------+--------+-------+----------+ `
月を持って
`+---------+-------+
| idmonth | month |
+---------+-------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
| 6 | 6 |
| 7 | 7 |
| 8 | 8 |
| 9 | 9 |
| 10 | 10 |
| 11 | 11 |
| 12 | 12 |
+---------+-------+ `
私は、idloanの回数、量および各月の合計の和の合計を取得するには、このクエリを使用し
、
` SELECT
m.month,
COUNT(l.idloan)AS m_count,
COALESCE(SUM(l.amount),0)AS amount,
COALESCE(SUM(l.total),0)AS total
FROM loan l RIGHT JOIN month m using(month)
GROUP BY m.month `
と出力された出力を得るためにこれをソートする方法
` +-------+---------+--------+-------+
| month | m_count | amount | total |
+-------+---------+--------+-------+
| 1 | 0 | 0 | 0 |
| 10 | 1 | 15000 | 16500 |
| 11 | 1 | 30000 | 3000 |
| 12 | 3 | 35000 | 51000 |
| 2 | 0 | 0 | 0 |
| 3 | 0 | 0 | 0 |
| 4 | 0 | 0 | 0 |
| 5 | 0 | 0 | 0 |
| 6 | 0 | 0 | 0 |
| 7 | 0 | 0 | 0 |
| 8 | 0 | 0 | 0 |
| 9 | 0 | 0 | 0 |
+-------+---------+--------+-------+ `
このように
` +-------+---------+--------+-------+
| month | m_count | amount | total |
+-------+---------+--------+-------+
| 1 | 0 | 0 | 0 |
| 2 | 0 | 0 | 0 |
| 3 | 0 | 0 | 0 |
| 4 | 0 | 0 | 0 |
| 5 | 0 | 0 | 0 |
| 6 | 0 | 0 | 0 |
| 7 | 0 | 0 | 0 |
| 8 | 0 | 0 | 0 |
| 9 | 0 | 0 | 0 |
| 10 | 1 | 15000 | 16500 |
| 11 | 1 | 30000 | 3000 |
| 12 | 3 | 35000 | 51000 |
+-------+---------+--------+-------+ `
である必要があります。月を整数で格納することをお勧めします。また、日付データ型 – Strawberry