Files
BMS/bms-back/sql/customer+supplier/customer_product_codes.sql
T
2026-09-22 11:20:51 +08:00

24 lines
1.2 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.
-- =====================================================================
-- 客户绑定产品:存储口径由「产品ID逗号串」改为「产品代码逗号串」
-- 背景:后续产品数据从旧数据库迁移导入,新旧库产品ID不一致,
-- 产品代码(product_code)在两库间稳定,故客户绑定关系改用代码。
-- 日期:2026-09-22
-- =====================================================================
-- 1. 列改名(保留原数据,CHANGE 仅改列名/注释)
ALTER TABLE bms_customer
CHANGE COLUMN product_ids product_codes varchar(1000) DEFAULT NULL
COMMENT '绑定产品代码(多个产品代码用逗号分隔,对接旧库产品编码)';
-- 2. 旧值(产品ID逗号串)按 bms_product 映射为产品代码逗号串
-- FIND_IN_SET 逐个匹配逗号串中的ID,GROUP_CONCAT 重新拼为代码串;
-- 匹配不到产品的ID会被置为NULL(脏数据,可在客户编辑页重新绑定)
UPDATE bms_customer c
SET c.product_codes = (
SELECT GROUP_CONCAT(p.product_code)
FROM bms_product p
WHERE FIND_IN_SET(p.product_id, c.product_codes)
)
WHERE c.product_codes IS NOT NULL
AND c.product_codes <> '';