I have a table that has two columns, startDate
and endDate
. Each day has its own capacity. Between these dates, user can get from 1 to n items. I am going to get number of items remaining for each day and show them to user. Is there any way I handle it with one query?
I am using spittet/php-mysql docker image and MySQL Version is 5.5.
id startDate endDate capacity
----------------------------------------------------
1 2019-05-01 2019-06-01 8
2 2019-06-21 2019-07-01 13
3 2019-07-11 2019-08-01 4
And the other table stored user data
id user_id item_id date number
----------------------------------------------------------------
1 1 1 2019-05-03 1
2 2 3 2019-07-13 4
Desired output:
item_id date available_items
-------------------------------------------------------
1 2019-05-01 8
1 2019-05-02 8
1 2019-05-03 7 <<-----------
1 2019-05-04 8
1 2019-05-05 8
...
1 2019-05-31 8
2 2019-06-21 13
2 2019-06-22 13
...
2 2019-06-30 13
3 2019-07-11 4
3 2019-07-12 4
3 2019-07-13 0 <<-----------
3 2019-07-14 4
...
3 2019-07-31 4
Best Answer
I suggest you create a calendar table with a couple of years of data. I'll just use one with sufficiently many rows for your example:
While we are at it, let's create some usable sample data instead of ascii-art tables:
To determine the capacity for each date:
Now you can subtract your userdata from that, perhaps easiest to do with a sub-select