You can execute a data-driven JOIN with a single request. Essentially, you would JOIN each subtable you need, and then select by smoothing the contents of the table you need. Assuming 1 is an Activity_drink, 2 is an Activity_eat, and 3 is an active_sport, and all the sub-tables have the content column that you want to get:
SELECT a.`timestamp`, CASE a.`activity_type` WHEN 1 THEN ad.`content` WHEN 2 THEN ae.`content` WHEN 3 THEN asp.`content` END AS content FROM activities AS a LEFT JOIN activity_drink AS ad ON (ad.`activity_id` = a.`activity_id`) LEFT JOIN activity_eat AS ae ON (ae.`activity_id` = a.`activity_id`) LEFT JOIN activity_sports AS asp ON (asp.`activity_id` = a.`activity_id`)
This will basically denormalize your tables at a specific time. You can also convert it to VIEW for easy access. This should not be too expensive, assuming that you have correctly configured the foreign keys, identifiers and / or UNIQUE indexes (MySQL will not see the corresponding rows in the table and will โignore themโ - selecting the NULL row). I did not test it quite correctly, since I did not have enough data and it should have been assumed, but the fragment should be basically functional.
However, I would like to mention that I personally fear that you should connect to the data. The correct way to normalize in this case is to find the largest common set of attributes and place them in the activities table. If necessary, you can add additional information to adjacent tables. Generally speaking, however, if the same data is used in several tables, you should probably transfer them to the main column if absolutely necessary.
source share