问题描述:
来源数据已经自己处理,只有三个字段存放到文本里。
原来的做法,是创建一个三个字段对应的、临时表然后load data infile都这个表
+-----------+---------------+------+-----+---------+-------+
| Field     | Type          | Null | Key | Default | Extra |
+-----------+---------------+------+-----+---------+-------+
| fldUserId | int(11)       | YES  |     | NULL    |       |
| fldCharId | int(11)       | YES  |     | NULL    |       |
| amount    | decimal(11,2) | YES  |     | NULL    |       |
+-----------+---------------+------+-----+---------+-------+
然后根据这个表和另外一个库的表联合查询出相关信息
将采集相关信息时使用了create table ... select的方式建立信息表,作为方法奖品(insert..select)的信息.
这次想不建立三个字段的临时表,而是直接建立采集信息表,然后load data infile来源信息到指点字段。
接着再根据这个表和另外一个库的表联合update这个采集信息表。
问题出现在然后load data infile来源信息到指定字段,并且使用字段分割符的情况下。
mysql>create table temp_presents_test_20120305(
fldUserName varchar(50),fldUserId int(11),fldCharId int(11),fldCharName varchar(20),fldServerID int(11),amount decimal(11,2)
);
load data infile '/home/diege/sj_cnc_0305.txt' into table temp_presents_test_20120305(fldUserId,fldCharId,amount) FIELDS TERMINATED BY ',';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FIELDS TERMINATED BY ','' at line
语法上有错误,具体不知。
分解语句测试。
文本文件使用默认的空格分隔,也可以导入。
load data infile '/home/diege/sj_tel_0305.txt' into table temp_presents_test_20120305(fldUserId,fldCharId,amount);
不指定导入的字段,指定导入的分隔符,也可以导入。
create table temp_presents_test1_20120305(
fldUserId int(11),fldCharId int(11),amount decimal(11,2)
);
load data infile '/home/diege/sj_cnc_0305.txt' into table temp_presents_test1_20120305 FIELDS TERMINATED BY ',';
把指定的字段放到FIELDS关键字之后就可以了
mysql> load data infile '/home/diege/sj_cnc_0305.txt' into table temp_presents_test_20120305 FIELDS TERMINATED BY ',' (fldUserId,fldCharId,amount);
 

线上操作用老的方法做了,现在在测试环境测试用update的想法:

操作有风险,所以先拉出来一个临时表来操作。
mysql>select a.fldUserName,a.fldUserId,a.fldCharId,a.fldCharName,a.fldServerID,b.amount from dbFSQxyZone.tblCharacter a,temp_presents_src_info_20120305 b where a.fldCharId=b.fldCharId group by fldUserName,fldCharId,fldServerID;

mysql> create table temp_tblCharacter_test select a.fldUserName,a.fldUserId,a.fldCharId,a.fldCharName,a.fldServerID from dbFSQxyZone.tblCharacter a,temp_presents_src_info_20120305 b where a.fldCharId=b.fldCharId group by fldUserName,fldCharId,fldServerID;
现在使用这个临时表来演练。
先用临时表select
mysql> select a.fldUserName,a.fldUserId,a.fldCharId,a.fldCharName,a.fldServerID,b.amount from temp_tblCharacter_test a,temp_presents_test_20120305 b where a.fldCharId=b.fldCharId group by fldUserName,fldCharId,fldServerID;
错误操作
mysql> update temp_tblCharacter_test a,temp_presents_test_20120305 b set a.fldUserName=b.fldUserName where a.fldCharId=b.fldCharId;
没有成功,这样来目标表数据没有变,来源的表的数据被目标表的空数据清空了。
所有,注意通过update多表联合修改,需要修改的表在签名,参考的表在后面。
MS还不能用group by
修改
mysql> update temp_presents_test_20120305 a, temp_tblCharacter_test b set a.fldUserName=b.fldUserName,a.fldCharName=b.fldCharName ,a.fldServerID=b.fldServerID where a.fldCharId=b.fldCharId;
验证
mysql> select * from temp_presents_test_20120305 limit 10;
mysql> select * from temp_tblCharacter_test limit 10;
目标数据表修改了数据,源数据表没有变化。