YZProjectCloud/docsql/update_cfg_id.sql

30 lines
1.1 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

-- 更新pro_mobile_attendance_data表的cfg_id根据project_id从attendance_cfg中找到默认配置ID
UPDATE pro_mobile_attendance_data pmad
INNER JOIN (
SELECT ac.project_id, ac.id AS cfg_id
FROM attendance_cfg ac
WHERE ac.is_default = 1 AND ac.enabled = 1
) ac ON pmad.project_id = ac.project_id
SET pmad.cfg_id = ac.cfg_id
WHERE pmad.att_device = 'device' AND pmad.cfg_id = 0;
-- 测试查询:查看更新效果
SELECT pmad.id, pmad.project_id, pmad.cfg_id, ac.id AS new_cfg_id
FROM pro_mobile_attendance_data pmad
LEFT JOIN (
SELECT ac.project_id, ac.id
FROM attendance_cfg ac
WHERE ac.is_default = 1 AND ac.enabled = 1
) ac ON pmad.project_id = ac.project_id
WHERE pmad.att_device = 'device' AND pmad.cfg_id = 0;
-- 分批更新(可选,适用于大数据量)
UPDATE pro_mobile_attendance_data pmad
INNER JOIN (
SELECT ac.project_id, ac.id AS cfg_id
FROM attendance_cfg ac
WHERE ac.is_default = 1 AND ac.enabled = 1
) ac ON pmad.project_id = ac.project_id
SET pmad.cfg_id = ac.cfg_id
WHERE pmad.att_device = 'device' AND pmad.cfg_id = 0
LIMIT 1000;