Click here to get back home

update values on inserting duplicate index

 HomeNewsGroups | Search

mailing.database.mysql - MySQL database issues discussed in this best MySQL group in USENET 

get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
update values on inserting duplicate index FangQ 07-22-2006
Posted by FangQ on July 22, 2006, 11:42 pm
Please log in for more thread options
hi

I am very new to mysql. I have a question about using the "on duplicate
update" clause with insert command.

my table "data" has two columns, field1 and field2, where field1 is the
index and is "unique".

when I run

insert ignore into `data` ( `field1`,`field2`)
values
(1,2)
(2,6)
(2,9)
(5,1)

I got
1,2
2,6
5,1

because the third value pair, (2,9) was ignored due to duplication in
field1.

here is what I want to do: when inserting new record with duplicate
index, I want the existing record gets updated. in the above case, I
want the output look like the following after the insert command:
1,2
2,9
5,1

after searching mysql online document, I only found the "on duplicate
update" clause can do something similar, however, all the examples only
show setting the duplicate record to somthing unrelated to the new
values.

I am wondering if this is possible with this command, or there are
other better choices?

thank you very much

Qianqian


Posted by Gordon Burditt on July 23, 2006, 12:19 am
Please log in for more thread options
show/hide quoted text

I commonly use a query like:

        insert into table(id, counter) values ($id, 1) on duplicate key
        update counter=counter+1;

(where $id gets a value substituted in to it). Every time I want to
count the occurrence of a $id, I run that query. If there's no record,
it makes one. If there is a record, it increments the counter.

show/hide quoted text

I don't understand how you got to the result from the original.

insert into `data` (field1, field2) values (2, 9) on duplicate key
update field2 = field2 + 3;

would accomplish what you stated but how you get there is unclear.

Incidentally, if what you want to do is REPLACE the whole record,
whether it existed previously or not, you can use REPLACE instead
of INSERT and forget ON DUPLICATE KEY UPDATE.

                                        Gordon L. Burditt

Posted by Michael Austin on July 23, 2006, 2:37 pm
Please log in for more thread options
Gordon Burditt wrote:
show/hide quoted text


Could you also use:

INSERT INTO table (f1,f2) VALUES (1,2) (2,6) (2,9) (5,1)
ON DUPLICATE KEY UPDATE f2=VALUES(f2);



--
Michael Austin.
DBA Consultant
Donations welcomed. Http://www.firstdbasource.com/donations.html
:)

Posted by Gordon Burditt on July 23, 2006, 2:56 pm
Please log in for more thread options
show/hide quoted text

That syntax won't work. Please state what it is you are trying
to accomplish.

                                        Gordon L. Burditt

Posted by FangQ on July 23, 2006, 6:38 pm
Please log in for more thread options
hi Gordon

indeed, REPLACE is exactly what I am looking for. thank you very much
for pointing out.

Qianqian

Gordon Burditt wrote:
show/hide quoted text


Similar ThreadsPosted
ON DUPLICATE KEY UPDATE with an old version of MySQL September 25, 2005, 11:38 am
Replication; Duplicate Entry on Update? April 27, 2006, 2:54 pm
INSERT ... SELECT ... ON DUPLICATE KEY UPDATE not working for me. February 8, 2006, 7:59 am
MySql UPDATE problem with duplicate rows May 14, 2006, 9:59 am
Never update index April 29, 2005, 10:35 am
CREATE TABLE SELECT, ON DUPLICATE KEY UPDATE, and UNION November 23, 2005, 11:20 am
Update a table with values in another January 18, 2006, 4:51 am
Full text index before or after non-clustered index December 20, 2006, 7:26 am
inserting unique value June 21, 2007, 9:20 am
Inserting variables in mySQL table! June 25, 2005, 9:11 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Driving a better car - Fuelzilla.com

Cabling site for homeowners and pros alike - Cabling-Design.com

Friends:

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap
Privacy Policy