SELECT *
FROM (
SELECT YEAR(`date`) AS `year`, MONTH(`date`) AS `month`, COUNT(*)
FROM `table1`
WHERE `date` BETWEEN :first_date AND :last_date
GROUP BY `year`, `month`
) AS `t1`
JOIN (
SELECT YEAR(`date`) AS `year`, MONTH(`date`) AS `month`, COUNT(*)
FROM `table2`
WHERE `date` BETWEEN :first_date AND :last_date
GROUP BY `year`, `month`
) AS `t2` ON `t2`.`year` = `t1`.`year` AND `t2`.`month` = `t1`.`month`
<ul onclick="console.log(event.target.getAttribute('data-value'))">
<li data-value="1">Test 1</li>
<li data-value="2">Test 2</li>
</ul>
SET @order = 0;
SELECT * FROM
(SELECT @order := @order+1 AS `order`, ...)
UNION
(SELECT @order := @order+1 AS `order`, ...)
ORDER BY `order`
Use of ORDER BY for individual SELECT statements implies nothing about the order in which the rows appear in the final result because UNION by default produces an unordered set of rows.
ORDER BY e.`date` < NOW(), ABS(DATEDIFF(e.`date`, NOW()))