auto_trade_sys/backend/database/update_timezone_to_beijing.sql
薇薇安 11e3532ac3 a
2026-01-17 20:23:49 +08:00

75 lines
2.6 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.

-- 将旧数据的时间更新为北京时间UTC+8
-- 假设旧数据存储的是UTC时间或服务器本地时间需要转换为北京时间
--
-- 注意此脚本假设旧数据的时间是UTC时间或服务器本地时间需要根据实际情况调整
-- 如果旧数据已经是北京时间,则不需要运行此脚本
--
-- 使用方法:
-- 1. 先备份数据库
-- 2. 根据实际情况调整时间偏移量CONVERT_TZ 的第二个参数)
-- 3. 运行此脚本
USE `auto_trade_sys`;
-- 检查当前时区设置
SELECT @@global.time_zone, @@session.time_zone;
-- 方法1如果旧数据是UTC时间直接加8小时转换为北京时间
-- 更新 entry_time入场时间
UPDATE `trades`
SET `entry_time` = DATE_ADD(`entry_time`, INTERVAL 8 HOUR)
WHERE `entry_time` IS NOT NULL
AND `entry_time` < '2025-01-01 00:00:00'; -- 只更新2025年之前的数据根据实际情况调整
-- 更新 exit_time平仓时间
UPDATE `trades`
SET `exit_time` = DATE_ADD(`exit_time`, INTERVAL 8 HOUR)
WHERE `exit_time` IS NOT NULL
AND `exit_time` < '2025-01-01 00:00:00'; -- 只更新2025年之前的数据根据实际情况调整
-- 方法2如果旧数据是服务器本地时间需要根据服务器时区调整
-- 例如如果服务器是UTC+0则加8小时如果服务器是UTC+5则加3小时
-- 请根据实际情况调整 INTERVAL 的值
--
-- UPDATE `trades`
-- SET `entry_time` = DATE_ADD(`entry_time`, INTERVAL 8 HOUR)
-- WHERE `entry_time` IS NOT NULL;
--
-- UPDATE `trades`
-- SET `exit_time` = DATE_ADD(`exit_time`, INTERVAL 8 HOUR)
-- WHERE `exit_time` IS NOT NULL;
-- 方法3如果MySQL支持时区转换函数需要加载时区数据
-- 首先需要加载时区数据mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql
--
-- UPDATE `trades`
-- SET `entry_time` = CONVERT_TZ(`entry_time`, 'UTC', 'Asia/Shanghai')
-- WHERE `entry_time` IS NOT NULL;
--
-- UPDATE `trades`
-- SET `exit_time` = CONVERT_TZ(`exit_time`, 'UTC', 'Asia/Shanghai')
-- WHERE `exit_time` IS NOT NULL;
-- 验证更新结果(查看最近几条记录的时间)
SELECT
id,
symbol,
entry_time,
exit_time,
status,
TIMESTAMPDIFF(HOUR, entry_time, COALESCE(exit_time, NOW())) as duration_hours
FROM `trades`
ORDER BY id DESC
LIMIT 10;
-- 统计信息
SELECT
COUNT(*) as total_trades,
COUNT(CASE WHEN entry_time IS NOT NULL THEN 1 END) as has_entry_time,
COUNT(CASE WHEN exit_time IS NOT NULL THEN 1 END) as has_exit_time,
MIN(entry_time) as earliest_entry,
MAX(entry_time) as latest_entry,
MIN(exit_time) as earliest_exit,
MAX(exit_time) as latest_exit
FROM `trades`;