MySQL Query on two tables containing different dates

I've a little issue I cannot handle very well.
I need to export some data from two tables. Both tables are containing only two fields. The first is a datetime, the second contains values.
I would like to graph those data grouping it by dates. Let me explain with an example.

As you can see the Time value is not equal. I would like to get values taken from both tables, group and graph them. The graph is not a problem. I would like, in example, take one value per table every 5 minutes.
Any help would be apreciated!

Grouping to 5 minutes (300 seconds). If multiple values fall into one interval take the maximum value.

select from_unixtime(U*300) as date,
max(if(T='A',val,NULL)) as A_VAL,
max(if(T='B',val,NULL)) as B_VAL
from (
select ceil(unix_timestamp(date)/300) as U, val, 'A' as T
from tableA
WHERE date BETWEEN '2016-08-26 12:20:00' AND '2016-08-26 12:55:00'
union all
select ceil(unix_timestamp(date)/300) as U, val, 'B' as T
from tableB
WHERE date BETWEEN '2016-08-26 12:20:00' AND '2016-08-26 12:55:00'
) A
group by U
order by U