Joywork/sql/area_price.sql
2026-05-22 21:21:54 +03:00

41 lines
1.6 KiB
SQL
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- Добавляем столбец area_price в complex_rooms
ALTER TABLE complex_rooms ADD COLUMN area_price DECIMAL(12,2) DEFAULT NULL;
-- Обновляем все существующие квартиры( для всех устанавливаем area_price считая total_amount / area)
UPDATE complex_rooms
SET area_price = total_amount / area
WHERE (area_price IS NULL OR area_price = 0)
AND area > 0;
-- Создание триггера, который при добавлении в базу новой записи complex_rooms будет автоматически считать
-- цену за квадратный метр area_price
-- Для этого в таблице complex_rooms создаем новый триггер где
-- Name calculate_area_price_before_insert
-- Время before
-- Собитые Insert
-- а определение SQL внизу
IF NEW.area IS NOT NULL AND NEW.total_amount IS NOT NULL AND NEW.area > 0 THEN
SET NEW.area_price = NEW.total_amount / NEW.area;
ELSE
SET NEW.area_price = NULL;
END IF;
--Обновление цени area_price при изменение в total_amount или area
-- Для этого в таблице complex_rooms создаем новый триггер где
-- Name calculate_area_price_before_update
-- Время before
-- Собитые update
-- а определение SQL внизу
IF NEW.area IS NOT NULL AND NEW.total_amount IS NOT NULL AND NEW.area > 0 THEN
SET NEW.area_price = NEW.total_amount / NEW.area;
ELSE
SET NEW.area_price = NULL;
END IF;