我有一个包含复合主键的表。我正在尝试从文本文件加载数据。 我需要将新值添加到表的原始值。
drop table if exists load_data;
CREATE TABLE `load_data` (
`zoneid` bigint(20) NOT NULL DEFAULT '0',
`creativeid` bigint(20) NOT NULL DEFAULT '0',
`count` int(11) DEFAULT NULL,
PRIMARY KEY (`zoneid`,`creativeid`)
) ENGINE=MyISAM;
insert into load_data values (1200, 2200, 4), (9200, 9200, 91);
mysql> select * from load_data;
+--------+------------+-------+
| zoneid | creativeid | count |
+--------+------------+-------+
| 1200 | 2200 | 4 |
| 9200 | 9200 | 91 |
+--------+------------+-------+
2 rows in set (0.00 sec)
# cat abc1.txt
1200 2200 8
9200 9200 7
以下加载数据为新值添加了5个计数,而不是主键列唯一组合的原始计数。
load data infile 'abc1.txt' replace into table load_data (zoneid, creativeid, @a) set count = @a+5 ;
mysql> select * from load_data;
+--------+------------+-------+
| zoneid | creativeid | count |
+--------+------------+-------+
| 1200 | 2200 | 13 |
| 9200 | 9200 | 12 |
+--------+------------+-------+
2 rows in set (0.00 sec)
当我尝试将值添加到相应区域和广告素材ID列的原始计数值时,我得到的是NULL而不是新的总数。
mysql> load data infile 'abc1.txt' replace into table load_data (zoneid, creativeid, @a) set count = @a+@count ;
Query OK, 4 rows affected (0.00 sec)
Records: 2 Deleted: 2 Skipped: 0 Warnings: 0
mysql> select * from load_data;
+--------+------------+-------+
| zoneid | creativeid | count |
+--------+------------+-------+
| 1200 | 2200 | NULL |
| 9200 | 9200 | NULL |
+--------+------------+-------+
2 rows in set (0.00 sec)
预期结果:
mysql> select * from load_data;
+--------+------------+-------+
| zoneid | creativeid | count |
+--------+------------+-------+
| 1200 | 2200 | 12 |
| 9200 | 9200 | 98 |
+--------+------------+-------+
答案 0 :(得分:1)
这是因为REPLACE强制首先删除记录。
要处理它,您可以临时添加新字段并从文件中加载count
数据。然后写UPDATE表来计算count
字段,然后删除temp。字段。