0

I have this table:

+----+--------+------------+-----------+------------------+
| id | amount | period     | player_id | payment_category |
+----+--------+------------+-----------+------------------+
|  3 |    100 | 2017-06-01 |         1 |                1 |
| 10 |    100 | 2017-06-01 |         2 |                1 |
|  6 |    100 | 2017-08-01 |         2 |                1 |
|  5 |    100 | 2017-10-01 |         3 |                1 |
|  2 |    100 | 2017-11-01 |         2 |                1 |
|  4 |    100 | 2017-11-01 |         1 |                1 |
|  1 |    100 | 2017-12-01 |         1 |                1 |
|  7 |    100 | 2017-12-01 |         1 |                1 |
|  9 |    100 | 2017-12-01 |         2 |                1 |
| 11 |    200 | 2017-12-01 |         1 |                3 |
+----+--------+------------+-----------+------------------+

Current result:

2017-06-01 2
2017-08-01 1
2017-10-01 1
2017-11-01 2
2017-12-01 3

Desired result:

2017-06-01 2
2017-07-01 0
2017-08-01 1
2017-09-01 0
2017-10-01 1
2017-11-01 2
2017-12-01 3

How can I do that? I tried with coalesce and ifnull. No luck.

I started with DQL:

    $query = $em->createQuery('
        SELECT COUNT(p.id) as total, p.period
        FROM AppBundle:Payment p
        WHERE p.period >= :fromTime
        AND p.period <= :toTime
        AND p.paymentCategory = 1
        GROUP BY p.period            
    ')

Of course this does not return the results for missing months ;) I'm looking for a method (doctrine or native mysql) which will somehow fill in the missing months. They can't be hardcoded - I need the results from last x months. Is it even possible or I should do it in PHP?

Tompo
  • 341
  • 1
  • 2
  • 11
  • Show us what you tried - both examples, if you can. The more you share with us, the faster and easier we'll be able to help you find the issue. – D.N. Dec 08 '17 at 19:44
  • You need to generate a table with all dates in the interval first: https://stackoverflow.com/questions/9295616/how-to-get-list-of-dates-between-two-dates-in-mysql-select-query – Radim Bača Dec 08 '17 at 20:07

0 Answers0