I have three tables linked between them following this scheme : A building have many statements which have many rooms. Each room has a type with a Enum field (Bathroom, Kitchen...) and an area.
Here is the SQL code :
CREATE TABLE `test_building` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`flags` int(10) unsigned NOT NULL DEFAULT '0',
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`residence_id` int(10) unsigned NOT NULL DEFAULT '0',
`status` tinyint(3) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `residence_id` (`residence_id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `test_settlement` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`building_id` int(10) unsigned NOT NULL DEFAULT '0',
`flags` int(10) unsigned NOT NULL DEFAULT '0',
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`residence_id` int(10) unsigned NOT NULL DEFAULT '0',
`status` tinyint(3) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `building_id` (`building_id`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `test_room` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`settlement_id` int(10) unsigned NOT NULL DEFAULT '0',
`flags` int(10) unsigned NOT NULL DEFAULT '0',
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`type` ENUM('Chambre', 'Cuisine', 'WC', 'Salon', 'Salle de bain') NOT NULL,
`area` tinyint(3) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `settlement_id` (`settlement_id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
The goal is to store in a 2D php array the listing of every building and, for each building, the sum of the areas by type.
To give an exemple the final array should look like this :
Array(
[Building 1] => array(
[KitchensArea] => 52
[BathrromsAreas] => 43
)
[Building 2] => array(
[KitchensArea] => 69
[BathrromsAreas] => 74
)
)
Any ideas about how to do that ? I'm like totally stuck, I think it's possible with only one query but i don't really mind if it takes many.
Thanks in advance !