if exists(select id from a where a.phonenum='123') then insert into b values ...
insert into @p_desctable(....) select ... from @p_srctable where updateBeginDate=@p_downdate and sequenceNumber=@p_downseq and not exists(select 1 from p_desctable d where d.KeyField=p_srctable.KeyField);
INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name [(col_name,...)]
VALUES ({expr | DEFAULT},...),(...),...
[ ON DUPLICATE KEY UPDATE col_name=expr, ... ]
我的问题表述有误,应该是先跟表tv_mobile进行比较,然后将数据插入新表tv_mobile_new中,如果记录重复就不插入表tv_mobile_new中,如果不重复就插入。
我现在的流程如下:
1、先在表tv_mobile中查询是否有此记录,select * from tv_mobile where section = '手机号码' 如果存在,就不插入,如果不存在就执行insert into tv_mobile_new value(,,,...);
2、现在存在的问题是在表tv_mobile中查询时时间过长。因为我要操作的数据有几千条,原表tv_mobile中有数据二十万条,我程序中用到的是循环操作,一条一条比较,再后再插入。
你是否可以先通过查询把二十万条记录需要的信息记录下来,再通过程序来去除相对应的记录,把没有重复的利用insert连接起来,批量插入,还有,你这里的二千条记录应该是在某一时刻去写入的,不会是无时无刻进入写入吧
应该是用一次SELECT查询从表中把二十万的条记录中的号码读取出来,放入数组,再利用程序来判断二千条数据里面哪些号码是存在于那个"数组"中的。
怎样让二行条记录跟原表一次性比较?
不用循环.
select ...
from @p_srctable
where updateBeginDate=@p_downdate and sequenceNumber=@p_downseq
and not exists(select 1 from p_desctable d
where d.KeyField=p_srctable.KeyField);