I have a table with composite primary key. I am trying to load data from a text file.
I need the new value to be added to the original value of the table.
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
The following load data is adding 5 counts to the new value and not the orginal count for unique combination of primary key columns.
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)
When I try to add the value to the original count value of respective zone and creative id columns, I get NULL instead of the new total.
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)
Expected results:
mysql> select * from load_data;
+--------+------------+-------+
| zoneid | creativeid | count |
+--------+------------+-------+
| 1200 | 2200 | 12 |
| 9200 | 9200 | 98 |
+--------+------------+-------+
That is because REPLACE forces to remove records firstly.
To handle it, you can temporary add new field and load
countdata from the file. Then write UPDATE table to calculatecountfield, and then remove temp. field.