473,379 Members | 1,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

INSERT... SELECT.. with ON DUPLICATE KEY... Is this possible?

Im trying to insert a bunch of rows into a table. If the row already
exists id like to update the row 'counter'. For example...

INSERT INTO table1
SELECT field1, field2
FROM table2
ON DUPLICATE KEY UPDATE field2 = 1

Is it possible to use both INSERT... SELECT... with ON DUPLICATE KEY?
I cant get it to work.

-Nick
Jul 20 '05 #1
8 5659
Nick wrote:
Yes, Im using 5.0 for Mac OSX. That feature would reaaallly help me out. -Nick


Okay, then can you be more specific regarding not being able to get the
feature to work. It looks like you are trying to set field2 to the
value 1 on a failed insert. I'll make a guess you meant to increment
the value of field2, in which case you should do this:

INSERT INTO table1
SELECT field1, field2
FROM table2
ON DUPLICATE KEY UPDATE field2 = field2 + 1

Refer to http://dev.mysql.com/doc/mysql/en/INSERT.html for more information.

Regards,
Bill K.
Jul 20 '05 #2
Nick wrote:
Is it possible to use both INSERT... SELECT... with ON DUPLICATE KEY?
I cant get it to work.


I haven't used it, but I noticed the docs say that this feature is new
as of MySQL 4.1.0. Are you using that version of MySQL or later?

Regards,
Bill K.
Jul 20 '05 #3
Sorry for the typo Bill. I tried using the above example you gave me
which returns the error message:

Syntax error or access violation, message from server:
You have an error in your SQL syntax. Check the manual that
corresponds to your MySQL server version for the right syntax to use.

The MySQL documentation link doesnt show the option for ON DUPLICATE
KEY for INSERT... SELECT...

Were you able to get it to work? -Nick
Jul 20 '05 #4
Yes, Im using 5.0 for Mac OSX. That feature would reaaallly help me out. -Nick
Jul 20 '05 #5
Nick wrote:
The MySQL documentation link doesnt show the option for ON DUPLICATE
KEY for INSERT... SELECT...
On page http://dev.mysql.com/doc/mysql/en/INSERT.html:

"If you use an INSERT ... VALUES statement with multiple value lists or
INSERT ... SELECT, the statement returns an information string in this
format:
...."
Were you able to get it to work? -Nick


No, I'm using MySQL 4.0.20. So I can't offer any advice from my own
experience. I agree that it's a useful feature, but it's not standard
SQL. Since it's not working, it might be easier to code around it the
old-fashioned way. That is, fetch the dataset you want to insert, and
filter out entries that already exist in the destination table.

Regards,
Bill K.
Jul 20 '05 #6
Nick wrote:
Yes, Im using 5.0 for Mac OSX. That feature would reaaallly help me out. -Nick


Okay, then can you be more specific regarding not being able to get the
feature to work. It looks like you are trying to set field2 to the
value 1 on a failed insert. I'll make a guess you meant to increment
the value of field2, in which case you should do this:

INSERT INTO table1
SELECT field1, field2
FROM table2
ON DUPLICATE KEY UPDATE field2 = field2 + 1

Refer to http://dev.mysql.com/doc/mysql/en/INSERT.html for more information.

Regards,
Bill K.
Jul 20 '05 #7
Sorry for the typo Bill. I tried using the above example you gave me
which returns the error message:

Syntax error or access violation, message from server:
You have an error in your SQL syntax. Check the manual that
corresponds to your MySQL server version for the right syntax to use.

The MySQL documentation link doesnt show the option for ON DUPLICATE
KEY for INSERT... SELECT...

Were you able to get it to work? -Nick
Jul 20 '05 #8
Nick wrote:
The MySQL documentation link doesnt show the option for ON DUPLICATE
KEY for INSERT... SELECT...
On page http://dev.mysql.com/doc/mysql/en/INSERT.html:

"If you use an INSERT ... VALUES statement with multiple value lists or
INSERT ... SELECT, the statement returns an information string in this
format:
...."
Were you able to get it to work? -Nick


No, I'm using MySQL 4.0.20. So I can't offer any advice from my own
experience. I agree that it's a useful feature, but it's not standard
SQL. Since it's not working, it might be easier to code around it the
old-fashioned way. That is, fetch the dataset you want to insert, and
filter out entries that already exist in the destination table.

Regards,
Bill K.
Jul 20 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Bill Kellaway | last post by:
Hi there - this should be fairly simple for someone. Basically I can't figure out how to pass the parameters from ASP to a Stored Procedure on SQL. Here's my code: I just need to help in...
1
by: Gary Lundquest | last post by:
It appears to me that MySQL version 4 returns an error messge when doing an Insert that results in duplicate entries. Version 3 did NOT return an error - it dropped the duplicate entries and ran...
2
by: Nick | last post by:
Im trying to insert a bunch of rows into a table. If the row already exists id like to update the row 'counter'. For example... INSERT INTO table1 SELECT field1, field2 FROM table2 ON...
18
by: Elroyskimms | last post by:
I have a table using an identity column as its Primary Key and two columns (table reduced for simplicity) EmployeeNumber and ArrivalTime. CREATE TABLE ( IDENTITY (1, 1) NOT NULL , (10)...
11
by: Jean-Christian Imbeault | last post by:
I have a table with a primary field and a few other fields. What is the fastest way to do an insert into that table assuming that sometimes I might try to insert a record with a duplicate primary...
8
by: Kragen Sitaker | last post by:
ERROR: Cannot insert a duplicate key into unique index pg_class_relname_nsp_index We've been getting this error in our application every once in a while --- typically once an hour to once a day,...
20
by: Mark Harrison | last post by:
So I have some data that I want to put into a table. If the row already exists (as defined by the primary key), I would like to update the row. Otherwise, I would like to insert the row. I've...
8
by: nano2k | last post by:
Hi Shortly, I keep invoices in a table. Occasionally, someone will fire the execution of a stored procedure (SP) that performs several UPDATEs against (potentially) all invoices OLDER than a...
2
by: mivey4 | last post by:
Okay I have 2 tables: Table A - holds a list of new hardware serial numbers and their corresponding model (no constraints or indexes) Table B - holds a distinct list of current serial numbers...
2
by: nano | last post by:
Does sql server have a way to handle errors in a sproc which would allow one to insert rows, ignoring rows which would create a duplicate key violation? I know if one loops one can handle the error...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.