-- 为 positions 表添加 total_cost 字段 -- 执行前请备份数据 -- 1. 添加字段 ALTER TABLE positions ADD COLUMN total_cost DECIMAL(18,4) DEFAULT 0; -- 2. 根据现有数据初始化 total_cost(shares * avg_price) UPDATE positions SET total_cost = shares * avg_price WHERE total_cost = 0 OR total_cost IS NULL; -- 3. 验证更新结果 SELECT id, stock_code, shares, avg_price, total_cost, shares * avg_price as calculated_cost FROM positions LIMIT 10; -- SQLite 版本(如果使用 SQLite) -- ALTER TABLE positions ADD COLUMN total_cost REAL DEFAULT 0; -- UPDATE positions SET total_cost = shares * avg_price;