m*o
2 楼
我用的是mysql,有一个table:
columnA, columnB
1,a
2,b
我要create一个batch insert,里面有可能已经有1,a 和2,b. 这种情况我不想再
insert进去了。我试着加了个unique key 在columnA+columnB,可是这样的话,一旦遇
到duplicate的数据,mysql就停止insert了。 请问怎样让mysql继续insert那些不
duplicate的数据?
columnA, columnB
1,a
2,b
我要create一个batch insert,里面有可能已经有1,a 和2,b. 这种情况我不想再
insert进去了。我试着加了个unique key 在columnA+columnB,可是这样的话,一旦遇
到duplicate的数据,mysql就停止insert了。 请问怎样让mysql继续insert那些不
duplicate的数据?
a*p
3 楼
小人都是戴眼镜的。
n*6
4 楼
没用过mysql,不知道以下几种方法是否对你有用。
Option1:
cursor
loop
If not exists (Select ... From ...)
Insert into ...
Option2:
Union, if not have to to use 'insert'.
Option1:
cursor
loop
If not exists (Select ... From ...)
Insert into ...
Option2:
Union, if not have to to use 'insert'.
B*g
9 楼
google is your best friend.
http://www.devshed.com/c/a/MySQL/Error-Handling-Examples/
发包子
【在 m******o 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: 我用的是mysql,有一个table:
: columnA, columnB
: 1,a
: 2,b
: 我要create一个batch insert,里面有可能已经有1,a 和2,b. 这种情况我不想再
: insert进去了。我试着加了个unique key 在columnA+columnB,可是这样的话,一旦遇
: 到duplicate的数据,mysql就停止insert了。 请问怎样让mysql继续insert那些不
: duplicate的数据?
http://www.devshed.com/c/a/MySQL/Error-Handling-Examples/
发包子
【在 m******o 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: 我用的是mysql,有一个table:
: columnA, columnB
: 1,a
: 2,b
: 我要create一个batch insert,里面有可能已经有1,a 和2,b. 这种情况我不想再
: insert进去了。我试着加了个unique key 在columnA+columnB,可是这样的话,一旦遇
: 到duplicate的数据,mysql就停止insert了。 请问怎样让mysql继续insert那些不
: duplicate的数据?
t*s
11 楼
if the version of mysql is 4.1 or later
you can use following insert syntax:
Insert ... On Duplicate Key Update
【在 m******o 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: 我用的是mysql,有一个table:
: columnA, columnB
: 1,a
: 2,b
: 我要create一个batch insert,里面有可能已经有1,a 和2,b. 这种情况我不想再
: insert进去了。我试着加了个unique key 在columnA+columnB,可是这样的话,一旦遇
: 到duplicate的数据,mysql就停止insert了。 请问怎样让mysql继续insert那些不
: duplicate的数据?
you can use following insert syntax:
Insert ... On Duplicate Key Update
【在 m******o 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: 我用的是mysql,有一个table:
: columnA, columnB
: 1,a
: 2,b
: 我要create一个batch insert,里面有可能已经有1,a 和2,b. 这种情况我不想再
: insert进去了。我试着加了个unique key 在columnA+columnB,可是这样的话,一旦遇
: 到duplicate的数据,mysql就停止insert了。 请问怎样让mysql继续insert那些不
: duplicate的数据?
z*3
15 楼
有个比较粗俗的方法,直接用程序写把code包在try{} catch(){} 里面,这样就算停止
了,但是下一个循环还是会调用try当中的代码。
了,但是下一个循环还是会调用try当中的代码。
B*g
17 楼
peng
http://www.mitbbs.com/article/Database/31141284_3.html
【在 f*****e 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: DECLARE CONTINUE HANDLER FOR SQLSTATE '23000'
http://www.mitbbs.com/article/Database/31141284_3.html
【在 f*****e 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: DECLARE CONTINUE HANDLER FOR SQLSTATE '23000'
a*x
18 楼
如果用A+B作primary key的话,可以考虑用
Insert ignore into
或者
Replace into
【在 m******o 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: 我用的是mysql,有一个table:
: columnA, columnB
: 1,a
: 2,b
: 我要create一个batch insert,里面有可能已经有1,a 和2,b. 这种情况我不想再
: insert进去了。我试着加了个unique key 在columnA+columnB,可是这样的话,一旦遇
: 到duplicate的数据,mysql就停止insert了。 请问怎样让mysql继续insert那些不
: duplicate的数据?
Insert ignore into
或者
Replace into
【在 m******o 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: 我用的是mysql,有一个table:
: columnA, columnB
: 1,a
: 2,b
: 我要create一个batch insert,里面有可能已经有1,a 和2,b. 这种情况我不想再
: insert进去了。我试着加了个unique key 在columnA+columnB,可是这样的话,一旦遇
: 到duplicate的数据,mysql就停止insert了。 请问怎样让mysql继续insert那些不
: duplicate的数据?
B*g
21 楼
I don't know much about mysql and I don't have an environment to test.
I am thinking this one will always insert an record. When dup key, update
one of the key to a new value then insert.
All above is guess.
【在 t*****s 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: no, there must be some code after "On Dulicate Key Update"
: but you can update an unimportant field there
I am thinking this one will always insert an record. When dup key, update
one of the key to a new value then insert.
All above is guess.
【在 t*****s 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: no, there must be some code after "On Dulicate Key Update"
: but you can update an unimportant field there
m*i
22 楼
Some straight forward way:
Put all your new data into a temp table X.
delete all rows in X which are already in the Dest table
delete X from X Join Dest where X.A = Dest.A and X.B = Dest.B
insert all records in X to the Dest table
insert into Dest select * from X
【在 m******o 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: 我用的是mysql,有一个table:
: columnA, columnB
: 1,a
: 2,b
: 我要create一个batch insert,里面有可能已经有1,a 和2,b. 这种情况我不想再
: insert进去了。我试着加了个unique key 在columnA+columnB,可是这样的话,一旦遇
: 到duplicate的数据,mysql就停止insert了。 请问怎样让mysql继续insert那些不
: duplicate的数据?
Put all your new data into a temp table X.
delete all rows in X which are already in the Dest table
delete X from X Join Dest where X.A = Dest.A and X.B = Dest.B
insert all records in X to the Dest table
insert into Dest select * from X
【在 m******o 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: 我用的是mysql,有一个table:
: columnA, columnB
: 1,a
: 2,b
: 我要create一个batch insert,里面有可能已经有1,a 和2,b. 这种情况我不想再
: insert进去了。我试着加了个unique key 在columnA+columnB,可是这样的话,一旦遇
: 到duplicate的数据,mysql就停止insert了。 请问怎样让mysql继续insert那些不
: duplicate的数据?
相关阅读
oracle connection problemcertificate? Help please.~{8_JVGsVz~}: how to create trigger on a nested table?How to Deploy EJB in Oracle Application Server?Where can I download Pro*C/C++ precompiler? Thanks a lot!网页中数据库格式显示问题请教关于在NT上以ASP访问INFORMIX数据库的问题Oracle question. thanks.Oracle序列使用问题!VB & ACCESS高手求助!!!Re: How to create a MS Access Database File using DAO in vc++???有关m# sql server text字段类型一问[转载] 怎么回事? CPU 总是全被占用了?SQL help.help on oracle!!Perl/CGI & MysqlOracle Server for Windows 2000 -- 新手求助如何安装Oracle内置包Re: How to concatenate NULL value with a string in SQL Server?Re: How to display Chinese text (query results) properly on web ??