I have following table structure, with many tables like this:
data_1:
+-------+--------+-------+
| views | clicks | date |
+-------+--------+-------+
| 29 | 1 | ..... |
| 458 | 9 | ..... |
+-------+--------+-------+
....
data_43:
+-------+--------+-------+
| views | clicks | date |
+-------+--------+-------+
| 0 | 0 | ..... |
| 0 | 0 | ..... |
+-------+--------+-------+
...
My question here is, how can I get all the sum values in one query?
I tried it with a simple join:
mysql> SELECT SUM(t1.views) data_1_views,
SUM(t1.clicks) data_1_clicks,
SUM(t2.views) data_43_views,
SUM(t2.clicks) data_43_clicks
FROM data_1 t1, data_43 t2;
But my result isn’t what I expected:
+--------------+---------------+---------------+----------------+
| data_1_views | data_1_clicks | data_43_views | data_43_clicks |
+--------------+---------------+---------------+----------------+
| NULL | NULL | NULL | NULL |
+--------------+---------------+---------------+----------------+
I expected it to be:
+--------------+---------------+---------------+----------------+
| data_1_views | data_1_clicks | data_43_views | data_43_clicks |
+--------------+---------------+---------------+----------------+
| 487 | 10 | 0 | 0 |
+--------------+---------------+---------------+----------------+
Anyone here who can help me with this query?
I am also interested if this is more performant than doing all this stuff in multiple queries
In this case multiple queries are fine especially if you are grouping many rows. There is virtually no benefit (and actually potential bugs) lumping these two tables in one query.
I would just make two queries