你的语句和方法没有问题。 你可以检查一下 select A,1,E from table1 where ID not in (select ID from table2) 这一句是否有结果返回,应该是你的所有的记录都已经在table2存在了。如果你不是想INSERT记录,而是要更新已存在记录则可以 update table2 inner join table1 on table2.ID=table1.ID sest table2.A=table1.A,table2.B=1,table2.C=table1.E;
一般在同步中即有INSERT也会有UPDATE,另外还要考虑DELELTEINSERT ,UPDATE 你可以合并为 insert table2(A,B,C) select A,1,E from table1 where ID not in (select ID from table2) ON DUPLICATE KEY UPDATE A=VALUES(A),B=1,C=Values(C)但DELETE你得另外写delete from table2 where id not in (select id from table1)
select A,1,E from table1 where ID not in (select ID from table2)这条有数据吗?
ACMAIN_CHM大哥回复真快!但很奇怪的是,我的table2里面一条数据都没有, 但select A,1,E from table1 where ID not in (select ID from table2) 这一句有结果返回啊,返回的是table1的所有记录,看这个逻辑也是应该返回记录吧?
select A,1,E from table1 where ID not in (select ID from table2) 这一句是否有结果返回,应该是你的所有的记录都已经在table2存在了。如果你不是想INSERT记录,而是要更新已存在记录则可以
update table2 inner join table1 on table2.ID=table1.ID
sest table2.A=table1.A,table2.B=1,table2.C=table1.E;
但select A,1,E from table1 where ID not in (select ID from table2)
这一句有结果返回啊,返回的是table1的所有记录,看这个逻辑也是应该返回记录吧?
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
`cityid` smallint(6) unsigned NOT NULL AUTO_INCREMENT ,
`from_city_id` mediumint(8) unsigned NOT NULL DEFAULT '0' ,
PRIMARY KEY (`cityid`),
KEY `from_city_id` (`from_city_id`)
) ENGINE=MyISAM AUTO_INCREMENT=286 DEFAULT CHARSET=utf8;INSERT INTO `table1` VALUES ('1', '110100');
INSERT INTO `table1` VALUES ('2', '510100');CREATE TABLE `table2` (
`cityid` smallint(6) NOT NULL ,
`source` smallint(6) NOT NULL ,
`sourceCity` smallint(8) NOT NULL,
PRIMARY KEY (`cityid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;insert table2(cityid,source,sourceCity) select cityid,1,from_city_id from table1 where cityid not in (select cityid from table2)结果有数据了很奇怪,但数据不同步大家可以跑跑看看数据库:mysql5.0.51a