<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: INSERT ON DUPLICATE KEY UPDATE and summary counters.</title>
	<atom:link href="http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/</link>
	<description>Percona&#039;s MySQL &#38; InnoDB performance and scalability blog</description>
	<lastBuildDate>Sat, 11 Feb 2012 16:45:54 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Steve Jackson</title>
		<link>http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/comment-page-1/#comment-820242</link>
		<dc:creator>Steve Jackson</dc:creator>
		<pubDate>Mon, 05 Sep 2011 06:10:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-820242</guid>
		<description>Given the table.

CREATE TABLE `test` (
  `id` int(11) NOT NULL,
  `md5` char(32) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `md5` (`md5`)
) ENGINE=InnoDB

INSERT IGNORE INTO test (md5) values (&#039;A&#039;)

1 row affected

INSERT IGNORE INTO test (md5) values (&#039;A&#039;)

Duplicate entry &#039;A&#039; for key &#039;md5&#039;

I think an incredibly useful feature here would be to return the innodb row id of the row which generated the duplicate key error. This would save us a select statement (either before or after the insert) to determine which row caused the dupe error. (I guess the unique row_id would not be of much help, so only return the value of PRIMARY if it is defined)

Is there something existing like this already? I dumped the mysqli connection object in php, and I see nothing like that. Insert_id of course, but that can only be used when there is an auto_increment column.

Someone will answer (why not just use on duplicate key update?) Well this is because i want &quot;on duplicate key return unique_row_id&quot;

Cheers

//Steve</description>
		<content:encoded><![CDATA[<p>Given the table.</p>
<p>CREATE TABLE `test` (<br />
  `id` int(11) NOT NULL,<br />
  `md5` char(32) DEFAULT NULL,<br />
  PRIMARY KEY (`id`),<br />
  UNIQUE KEY `md5` (`md5`)<br />
) ENGINE=InnoDB</p>
<p>INSERT IGNORE INTO test (md5) values (&#8216;A&#8217;)</p>
<p>1 row affected</p>
<p>INSERT IGNORE INTO test (md5) values (&#8216;A&#8217;)</p>
<p>Duplicate entry &#8216;A&#8217; for key &#8216;md5&#8242;</p>
<p>I think an incredibly useful feature here would be to return the innodb row id of the row which generated the duplicate key error. This would save us a select statement (either before or after the insert) to determine which row caused the dupe error. (I guess the unique row_id would not be of much help, so only return the value of PRIMARY if it is defined)</p>
<p>Is there something existing like this already? I dumped the mysqli connection object in php, and I see nothing like that. Insert_id of course, but that can only be used when there is an auto_increment column.</p>
<p>Someone will answer (why not just use on duplicate key update?) Well this is because i want &#8220;on duplicate key return unique_row_id&#8221;</p>
<p>Cheers</p>
<p>//Steve</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott</title>
		<link>http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/comment-page-1/#comment-778829</link>
		<dc:creator>Scott</dc:creator>
		<pubDate>Tue, 19 Oct 2010 15:16:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-778829</guid>
		<description>The field2 line above is a mess... it should be

field2= CASE WHEN field2â€™$field1â€² THEN â€˜$field3â€² ELSE field3 // this does not work off the existing (old) field1 value!</description>
		<content:encoded><![CDATA[<p>The field2 line above is a mess&#8230; it should be</p>
<p>field2= CASE WHEN field2â€™$field1â€² THEN â€˜$field3â€² ELSE field3 // this does not work off the existing (old) field1 value!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott</title>
		<link>http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/comment-page-1/#comment-778826</link>
		<dc:creator>Scott</dc:creator>
		<pubDate>Tue, 19 Oct 2010 15:05:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-778826</guid>
		<description>With ON DUPLICATE KEY UPDATE is there some sort of temp table involved with the old and new values?
I need access to the old values on update, but only want to hit the database once.
It somewhat works with CASE WHEN statements to catch high and low values by day.

field1= CASE WHEN field1&gt;&#039;$field1&#039; THEN &#039;$field1&#039; ELSE field1   // this gets a low value
field2= CASE WHEN field2&#039;$field1&#039; THEN &#039;$field3&#039; ELSE field3   // this does not work off the existing (old) field1 value!

This should update field3 when field1 changes. But it does not.

field3= CASE WHEN field1=&#039;$field1&#039; THEN &#039;$field3&#039; ELSE field3   // if field3 is time you will only see the most recent value match of field1

This does work but it looks to see if field1 is equal to the new value. 
The problem here is that I can only have the last match, I would like to have the first match in some cases.

It seems that old and new values are only available within a given field. Is that so?

Is there another way to have old/existing values available from one field in the case switch or value of another field.

Thanks.</description>
		<content:encoded><![CDATA[<p>With ON DUPLICATE KEY UPDATE is there some sort of temp table involved with the old and new values?<br />
I need access to the old values on update, but only want to hit the database once.<br />
It somewhat works with CASE WHEN statements to catch high and low values by day.</p>
<p>field1= CASE WHEN field1&gt;&#8217;$field1&#8242; THEN &#8216;$field1&#8242; ELSE field1   // this gets a low value<br />
field2= CASE WHEN field2&#8242;$field1&#8242; THEN &#8216;$field3&#8242; ELSE field3   // this does not work off the existing (old) field1 value!</p>
<p>This should update field3 when field1 changes. But it does not.</p>
<p>field3= CASE WHEN field1=&#8217;$field1&#8242; THEN &#8216;$field3&#8242; ELSE field3   // if field3 is time you will only see the most recent value match of field1</p>
<p>This does work but it looks to see if field1 is equal to the new value.<br />
The problem here is that I can only have the last match, I would like to have the first match in some cases.</p>
<p>It seems that old and new values are only available within a given field. Is that so?</p>
<p>Is there another way to have old/existing values available from one field in the case switch or value of another field.</p>
<p>Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dudeson</title>
		<link>http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/comment-page-1/#comment-778343</link>
		<dc:creator>Dudeson</dc:creator>
		<pubDate>Sun, 17 Oct 2010 06:06:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-778343</guid>
		<description>Beautiful function. I was on the verge of using a conditional update statement until i discovered this. Anyone knows if there is a mysql function that will shift all records key id down? by adding 1 when I add a new key, say 0 on the TOP of the table?</description>
		<content:encoded><![CDATA[<p>Beautiful function. I was on the verge of using a conditional update statement until i discovered this. Anyone knows if there is a mysql function that will shift all records key id down? by adding 1 when I add a new key, say 0 on the TOP of the table?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian May</title>
		<link>http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/comment-page-1/#comment-772354</link>
		<dc:creator>Brian May</dc:creator>
		<pubDate>Tue, 24 Aug 2010 02:57:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-772354</guid>
		<description>well, i definetly using INSERT ON DUPLICATE KEY UPDATE rather than REPLACE INTO
it&#039;s for performance and code reading</description>
		<content:encoded><![CDATA[<p>well, i definetly using INSERT ON DUPLICATE KEY UPDATE rather than REPLACE INTO<br />
it&#8217;s for performance and code reading</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jayapal Chandran</title>
		<link>http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/comment-page-1/#comment-769239</link>
		<dc:creator>Jayapal Chandran</dc:creator>
		<pubDate>Tue, 13 Jul 2010 14:19:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-769239</guid>
		<description>I HAVE MISSED THIS FOR TWO YEARS. OH WHAT A S... . ... WHAT EVER.
and now... just now found it while searching thinking why not there be an option like this... and here i am...

how to do like this

insert into (field1, field2, field3) values (1,2,3),(1,2,3) and so including on duplicate update. because i want insert/update many records from am json source.
This should have been posted in a forum but since it deals my problem exactly i thought of posting it here.</description>
		<content:encoded><![CDATA[<p>I HAVE MISSED THIS FOR TWO YEARS. OH WHAT A S&#8230; . &#8230; WHAT EVER.<br />
and now&#8230; just now found it while searching thinking why not there be an option like this&#8230; and here i am&#8230;</p>
<p>how to do like this</p>
<p>insert into (field1, field2, field3) values (1,2,3),(1,2,3) and so including on duplicate update. because i want insert/update many records from am json source.<br />
This should have been posted in a forum but since it deals my problem exactly i thought of posting it here.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kn33ch41</title>
		<link>http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/comment-page-1/#comment-768263</link>
		<dc:creator>kn33ch41</dc:creator>
		<pubDate>Thu, 01 Jul 2010 21:04:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-768263</guid>
		<description>A note for those of you who may have problems updating a row that does in fact exist in the table, and so are always seeing a new row inserted: 

In order for this to work, the PRIMARY KEY defined for the table should only contain one column--or if it has multiple columns--the data you&#039;re going to insert/update for the given columns should be identical to the columns defined in the PRIMARY KEY. 

To illustrate:

PRIMARY_KEY(`id`, `timestamp`) == a single key (but with two values, thus more unique)

The `id` is very likely always going to be unique. However, adding the `timestamp` to the primary key makes it even more unique; so, if the `id` exists in the table and you simply want to update the `timestamp`, the new value for the timestamp you&#039;re providing will mean that `id` matches but `timestamp` does not, because you&#039;ve defined the primary key to be `id` and `timestamp`, which means BOTH values you&#039;re inserting/updating must equal the values currently in the database to make the primary key match and therefore update the row instead of insert a new row with the same `id`, but different `timestamp`.</description>
		<content:encoded><![CDATA[<p>A note for those of you who may have problems updating a row that does in fact exist in the table, and so are always seeing a new row inserted: </p>
<p>In order for this to work, the PRIMARY KEY defined for the table should only contain one column&#8211;or if it has multiple columns&#8211;the data you&#8217;re going to insert/update for the given columns should be identical to the columns defined in the PRIMARY KEY. </p>
<p>To illustrate:</p>
<p>PRIMARY_KEY(`id`, `timestamp`) == a single key (but with two values, thus more unique)</p>
<p>The `id` is very likely always going to be unique. However, adding the `timestamp` to the primary key makes it even more unique; so, if the `id` exists in the table and you simply want to update the `timestamp`, the new value for the timestamp you&#8217;re providing will mean that `id` matches but `timestamp` does not, because you&#8217;ve defined the primary key to be `id` and `timestamp`, which means BOTH values you&#8217;re inserting/updating must equal the values currently in the database to make the primary key match and therefore update the row instead of insert a new row with the same `id`, but different `timestamp`.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Artis</title>
		<link>http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/comment-page-1/#comment-678705</link>
		<dc:creator>Artis</dc:creator>
		<pubDate>Tue, 17 Nov 2009 14:32:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-678705</guid>
		<description>If I am collecting banner display statistics for each day, then I have table `ad_stat` with fields:
id, banner_id, date, views
with (banner_id, date) as unique

I use insert into ad_stat (banner_id, date, views) values ($banner_id, &#039;$date&#039;, 1) ON DUPLICATE KEY UPDATE views=views+1

So this query firstly tries to insert and only then to Update - is there opposite way like:
UPDATE ... ON NO ROWS INSERT ???
Because there are 5 inserts daily and 50 000 updates - wouldn&#039;t it somehow increase performance?</description>
		<content:encoded><![CDATA[<p>If I am collecting banner display statistics for each day, then I have table `ad_stat` with fields:<br />
id, banner_id, date, views<br />
with (banner_id, date) as unique</p>
<p>I use insert into ad_stat (banner_id, date, views) values ($banner_id, &#8216;$date&#8217;, 1) ON DUPLICATE KEY UPDATE views=views+1</p>
<p>So this query firstly tries to insert and only then to Update &#8211; is there opposite way like:<br />
UPDATE &#8230; ON NO ROWS INSERT ???<br />
Because there are 5 inserts daily and 50 000 updates &#8211; wouldn&#8217;t it somehow increase performance?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Baron Schwartz</title>
		<link>http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/comment-page-1/#comment-575505</link>
		<dc:creator>Baron Schwartz</dc:creator>
		<pubDate>Fri, 05 Jun 2009 14:04:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-575505</guid>
		<description>VALUES(fld2) should go there.</description>
		<content:encoded><![CDATA[<p>VALUES(fld2) should go there.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jesse</title>
		<link>http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/comment-page-1/#comment-575381</link>
		<dc:creator>Jesse</dc:creator>
		<pubDate>Fri, 05 Jun 2009 11:43:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-575381</guid>
		<description>Hello,

I have a dilema with INSERT (fld1, fld2, fld3) VALUES(&#039;1&#039;,&#039;1a&#039;,&#039;1b&#039;),(&#039;2&#039;,&#039;2a&#039;,&#039;2b&#039;),(&#039;3&#039;,&#039;3a&#039;,&#039;3b&#039;) ON DUPLICATE KEY UPDATE fld2 = ??? ( I want this to be the value that would be inserted ie &#039;2a&#039;)  (fld1 is a unique index) 

What should go in the ??? spot?

Thanks.</description>
		<content:encoded><![CDATA[<p>Hello,</p>
<p>I have a dilema with INSERT (fld1, fld2, fld3) VALUES(&#8217;1&#8242;,&#8217;1a&#8217;,&#8217;1b&#8217;),(&#8217;2&#8242;,&#8217;2a&#8217;,&#8217;2b&#8217;),(&#8217;3&#8242;,&#8217;3a&#8217;,&#8217;3b&#8217;) ON DUPLICATE KEY UPDATE fld2 = ??? ( I want this to be the value that would be inserted ie &#8217;2a&#8217;)  (fld1 is a unique index) </p>
<p>What should go in the ??? spot?</p>
<p>Thanks.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

