I've been doing a lot of searching for some way to create a "Foreach" of sorts with a MySQL query.
I know how I'd go about this if it were PHP or Java, but I want this to be all MySQL.
So, I'm going to create a sort of 'theoretical' MySQL situation, since the original query has a lot of unnecessary information that I feel confuses the scope of the question.
What I'm envisioning is something that works in this sort of way (This will be an ugly mess of php and mysql pseudo code.)
@rooms = ('master_bedroom'=>'MBS_MASTER_BEDROOM_SIZE',
'bed_room2' = 'B2S_BR_2_SIZE')
FOREACH(@rooms as @room=>@column_name)
{
UPDATE `relative_room_sizes` SET area = CALCULATE_AREA(`buildings`.@column_name)
WHERE `idx_computed_values`.`room_type` = @room
AND `idx_computed_values`.`computed_size` = 'large'
}
Am I just taking things too far, or is there some very basic feature of MySQL that I'm just having a lot of trouble discovering? I did a lot of searching here but any sort of foreach tends to relate back to using an actual programming language to take care of this sort of dirty work.
Edit: Here's kind of the idea of what I want to achieve in the procedure, but I did it in PHP because... it's what I know :)... Essentially I want to be able have a procedure that does what this PHP code will output, and hopefully in just as compact a manner.
<?php
$rooms = array(
'master_bedroom' => 'MBS_MASTER_BEDROOM_SIZE',
'bed_room2' => 'B2S_BR_2_SIZE',
'bed_room3' => 'B3S_BR_3_SIZE',
'bed_room4' => 'B4S_BR_4_SIZE',
'living_room' => 'LRS_LIVING_ROOM_SIZE',
'dining_room' => 'DRS_DINING_ROOM_SIZE ',
'kitchen' => 'KTS_KITCHEN_SIZE',
'family_room' => 'FRS_FAMILY_ROOM_SIZE');
$room_sizes = array('large','small');
$table_with_properties = 'idx_search';
?>
<?foreach($rooms as $room_type=>$column_name):?>
<?foreach ($room_sizes as $room_size):?>
#Initialize field in case it isn't already declared
INSERT INTO `idx_computed_values` (`room_type`,`computed_size`,`area`)
values ('<?=$room_type?>','<?=$room_size?>',0)
ON DUPLICATE KEY UPDATE `computed_size` = `computed_size`;
UPDATE `idx_computed_values` SET area =
(SELECT AVG(CALCULATE_AREA(`<?=$table_with_properties?>`.<?=$column_name?>))<? if($room_size == 'large'):?>+<?php endif;?><? if($room_size == 'small'):?>-<?php endif;?>
(0.6 * STDDEV(CALCULATE_AREA(`idx_search`.<?=$column_name?>)))
FROM `<?=$table_with_properties?>`
WHERE `<?=$column_name?>` IS NOT NULL )
WHERE `idx_computed_values`.`room_type` = '<?=$room_type?>'
AND `idx_computed_values`.`computed_size` = '<?=$room_size?>';
<? endforeach;?>
<? endforeach;?>