Files
pj_service_cloud/db/20260930_backfill_mch_manage_discounts_rate.sql
T
2026-09-30 15:40:04 +08:00

33 lines
1.7 KiB
SQL
Raw 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.
-- 回填 mch_manage.discounts_rate:
-- 从 discounts_price 文案(如"享9.5折"、"享菜品7.8折(不含酒水)"、"全场95折")中提取"折"前面的数字,
-- 折算规则与 discounts_rate 语义(1-99,10=1折)一致:
-- 小数或个位整数("9.5折"->95、"9折"->90、"0.5折"->5) 乘以10;
-- 两位整数("95折")本身即百分比,保持原值 -> 95。
-- 仅填充 discounts_rate 为空的行,已配置的值不覆盖;提取不到"数字+折"或折算结果不在 1-99 的行跳过。
-- ① 预览:执行前先跑这条,人工确认折算结果
SELECT id, name, discounts_price,
ROUND(CAST(REPLACE(REGEXP_SUBSTR(discounts_price, '[0-9]+(\\.[0-9]+)?折'), '折', '') AS DECIMAL(10, 2))
* IF(CAST(REPLACE(REGEXP_SUBSTR(discounts_price, '[0-9]+(\\.[0-9]+)?折'), '折', '') AS DECIMAL(10, 2)) < 10, 10, 1)) AS will_rate
FROM mch_manage
WHERE discounts_rate IS NULL
AND discounts_price REGEXP '[0-9]+(\\.[0-9]+)?折';
-- ② 执行回填
UPDATE mch_manage m
JOIN (SELECT id,
ROUND(CAST(REPLACE(REGEXP_SUBSTR(discounts_price, '[0-9]+(\\.[0-9]+)?折'), '折', '') AS DECIMAL(10, 2))
* IF(CAST(REPLACE(REGEXP_SUBSTR(discounts_price, '[0-9]+(\\.[0-9]+)?折'), '折', '') AS DECIMAL(10, 2)) < 10,
10, 1)) AS rate
FROM mch_manage
WHERE discounts_rate IS NULL
AND discounts_price REGEXP '[0-9]+(\\.[0-9]+)?折') t ON t.id = m.id
SET m.discounts_rate = t.rate
WHERE t.rate BETWEEN 1 AND 99;
-- ③ 核对:回填结果与原文对照
SELECT id, name, discounts_price, discounts_rate
FROM mch_manage
WHERE discounts_rate IS NOT NULL
ORDER BY update_time DESC;