简体   繁体   中英

How to sort results in MySQL right join?

I am trying to get monthly data of whole year using MySQL using below two tables,

  1. loan
  2. month

loan

` +--------+--------+-------+----------+
| idloan | amount | tot   | interest |
+--------+--------+-------+----------+
|      3 |  10000 | 15000 |       50 |
|      4 |   5000 |  6000 |       10 |
|      5 |  20000 | 30000 |       10 |
|      6 |  30000 |  3000 |       10 |
|      7 |  15000 | 16500 |       10 |
+--------+--------+-------+----------+ `

month

`+---------+-------+
| 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    |
+---------+-------+ `

I used this query to get, count of idloan, sum of amount and sum of total of each month,

` 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 `

and the output is

` +-------+---------+--------+-------+
| 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 |
+-------+---------+--------+-------+ `

My question is how can I sort this to get output like this

` +-------+---------+--------+-------+
| 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 |
+-------+---------+--------+-------+ `

I believe you just need an order by clause. So your query should be

 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
ORDER BY CONVERT(m.month,UNSIGNED INTEGER)

If possible you should change the data type of month column of month table to INT.

Anyways you could still cast/convert the string to integer in your order by clause.

Something like below

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 
ORDER BY CONVERT(m.month,UNSIGNED INTEGER)

Would recommend you to change the data type rather than using CONVERT in your order by clause.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM