I have a database MySQL 8.0 with 12 tables (one of each month) with information about clients. All tables have a Client ID and the amount of money that they save. For example, for two months:
+------------------+
+ January +
+------------------+
+ ClienID | Amount +
+------------------+
+ qwer23 | 23 +
+------------------+
+ December +
+------------------+
+ ClienID | Amount +
+------------------+
+ qwer23 | 15 +
And I want to get a table with the ClientID and the Amount of each month. Like this
+------------------+----------------+----------+
+ ClienID | January| | December |
+------------------+----------------+----------+
+ qwer23 | 23 | | 15 |
I search some options, but I’m not sure how to use JOIN or GROUP+BY.
2
Answers
Thanks for all, I solve my problem with the following code:
Note: The original table is in spanish
You have one table for each of the twelve months, and a client can have up to one row in each table. To get a result row per client with all their monthly amounts, you’d full outer join all tables on the client ID. MySQL, however, still doesn’t support full outer joins.
As the tables have a client ID, I assume there is also a client table these IDs are linking to. So, just select the clients from the clients table and outer join the months:
If you don’t have a client table (which seems unlikely), you can create one on-the-fly, by replacing
by