<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>UK Specialists in Ruby on Rails, Exalead, AWS, Consultancy - Conditional duplicate key updates with MySQL Comments</title>
  <id>tag:thewebfellas.com,2012:/blog/2010/1/18/conditional-duplicate-key-updates-with-mysql/comments</id>
  <generator uri="http://mephistoblog.com" version="0.7.3">Mephisto Noh-Varr</generator>
  <link href="http://thewebfellas.com/blog/2010/1/18/conditional-duplicate-key-updates-with-mysql/comments.xml" rel="self" type="application/atom+xml"/>
  <link href="/blog/2010/1/18/conditional-duplicate-key-updates-with-mysql" rel="alternate" type="text/html"/>
  <updated>2011-12-05T14:27:31Z</updated>
  <entry xml:base="http://thewebfellas.com/">
    <author>
      <name>James</name>
    </author>
    <id>tag:thewebfellas.com,2010-01-18:12729:31500</id>
    <published>2011-11-29T21:05:26Z</published>
    <updated>2011-11-29T21:05:26Z</updated>
    <category term="Blog"/>
    <link href="http://thewebfellas.com/blog/2010/1/18/conditional-duplicate-key-updates-with-mysql" rel="alternate" type="text/html"/>
    <title>Comment on 'Conditional duplicate key updates with MySQL' by James</title>
<content type="html">&lt;p&gt;I was trying to figure out a way for multiple listeners that may receive some of the same events to be able to automatically de-duplicate them without having to use transactions (not supported by my engine) or an IPC/HA mechanism between the listeners (unnecessarily complex).  Great post, still solving problems almost 2 years later!&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://thewebfellas.com/">
    <author>
      <name>Rob Anderton</name>
    </author>
    <id>tag:thewebfellas.com,2010-01-18:12729:30810</id>
    <published>2011-10-28T22:45:23Z</published>
    <updated>2011-10-28T22:45:23Z</updated>
    <category term="Blog"/>
    <link href="http://thewebfellas.com/blog/2010/1/18/conditional-duplicate-key-updates-with-mysql" rel="alternate" type="text/html"/>
    <title>Comment on 'Conditional duplicate key updates with MySQL' by Rob Anderton</title>
<content type="html">&lt;p&gt;That's the point of this story - to show a workaround that does work in MySQL :)&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://thewebfellas.com/">
    <author>
      <name>pol2095</name>
    </author>
    <id>tag:thewebfellas.com,2010-01-18:12729:30807</id>
    <published>2011-10-28T14:40:43Z</published>
    <updated>2011-10-28T14:40:43Z</updated>
    <category term="Blog"/>
    <link href="http://thewebfellas.com/blog/2010/1/18/conditional-duplicate-key-updates-with-mysql" rel="alternate" type="text/html"/>
    <title>Comment on 'Conditional duplicate key updates with MySQL' by pol2095</title>
<content type="html">&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;INSERT INTO daily_events (created_on, last_event_id, last_event_created_at)
VALUES ('2010-01-19', 23, '2010-01-19 10:23:11')
ON DUPLICATE KEY UPDATE
last_event_id = VALUES(last_event_id),
last_event_created_at = VALUES(last_event_created_at)
WHERE last_event_created_at &amp;lt; VALUES(last_event_created_at);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;WHERE doesn't work in mysql&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://thewebfellas.com/">
    <author>
      <name>Rob Anderton</name>
    </author>
    <id>tag:thewebfellas.com,2010-01-18:12729:30799</id>
    <published>2011-10-28T10:34:52Z</published>
    <updated>2011-10-28T10:34:52Z</updated>
    <category term="Blog"/>
    <link href="http://thewebfellas.com/blog/2010/1/18/conditional-duplicate-key-updates-with-mysql" rel="alternate" type="text/html"/>
    <title>Comment on 'Conditional duplicate key updates with MySQL' by Rob Anderton</title>
<content type="html">&lt;p&gt;@rakesh: if the insert or update should only happen when the condition is true, then that sounds like a slightly different problem and the condition should be added as a &lt;code&gt;WHERE&lt;/code&gt; clause on the &lt;code&gt;SELECT&lt;/code&gt;, so something like:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;sql&quot;&gt;insert into maintenance.sap_promotions_item_level 
select * from pos.promotions_item_level 
WHERE (pos.promotions_item_level.modified_on &gt; select max(modified_on) from maintenance.sap_promotions_item_level)
ON DUPLICATE KEY  UPDATE 
maintenance.sap_promotions_item_level.SCALING_ID=pos.promotions_item_level.SCALING_ID,
maintenance.sap_promotions_item_level.DISC_INDICATOR=pos.promotions_item_level.DISC_INDICATOR,
maintenance.sap_promotions_item_level.QTY_QUALIFIER=pos.promotions_item_level.QTY_QUALIFIER,
maintenance.sap_promotions_item_level.DISCOUNT_PERCENT=pos.promotions_item_level.DISCOUNT_PERCENT,
maintenance.sap_promotions_item_level.PRICE_OFF_VALUE=pos.promotions_item_level.PRICE_OFF_VALUE,
maintenance.sap_promotions_item_level.PROMO_PRICE_VALUE=pos.promotions_item_level.PROMO_PRICE_VALUE,
maintenance.sap_promotions_item_level.FGOOD1_CODE=pos.promotions_item_level.FGOOD1_CODE,
maintenance.sap_promotions_item_level.FGOOD1_QTY=pos.promotions_item_level.FGOOD1_QTY,
maintenance.sap_promotions_item_level.FGOOD2_CODE=pos.promotions_item_level.FGOOD2_CODE,
maintenance.sap_promotions_item_level.FGOOD2_QTY=pos.promotions_item_level.FGOOD2_QTY,
maintenance.sap_promotions_item_level.FGOOD3_CODE=pos.promotions_item_level.FGOOD3_CODE,
maintenance.sap_promotions_item_level.FGOOD3_QTY=pos.promotions_item_level.FGOOD3_QTY,
maintenance.sap_promotions_item_level.FGOOD4_CODE=pos.promotions_item_level.FGOOD4_CODE,
maintenance.sap_promotions_item_level.FGOOD4_QTY=pos.promotions_item_level.FGOOD4_QTY,
maintenance.sap_promotions_item_level.VALID_FROM_DATE=pos.promotions_item_level.VALID_FROM_DATE,
maintenance.sap_promotions_item_level.VALID_TO_DATE=pos.promotions_item_level.VALID_TO_DATE,
maintenance.sap_promotions_item_level.modified_on=pos.promotions_item_level.modified_on;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;But I may have misunderstood your situation.&lt;/p&gt;

&lt;p&gt;@ionut: do you want one visit for a specific visitor per day? To handle the seen flag you'd &lt;code&gt;INSERT&lt;/code&gt; a zero into the field and then set it to 1 in the &lt;code&gt;ON DUPLICATE KEY UPDATE&lt;/code&gt; section. I'm guessing to handle the one per day, you may need to look at a subquery in the &lt;code&gt;IF&lt;/code&gt; that checks if any records exist for the given date/visitor ID combination, but as with rakesh I may well have misunderstood what you're trying to do!&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://thewebfellas.com/">
    <author>
      <name>ionut</name>
    </author>
    <id>tag:thewebfellas.com,2010-01-18:12729:30618</id>
    <published>2011-10-20T10:03:15Z</published>
    <updated>2011-10-20T10:03:15Z</updated>
    <category term="Blog"/>
    <link href="http://thewebfellas.com/blog/2010/1/18/conditional-duplicate-key-updates-with-mysql" rel="alternate" type="text/html"/>
    <title>Comment on 'Conditional duplicate key updates with MySQL' by ionut</title>
<content type="html">&lt;p&gt;hi maybe you can help
i have the table&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;sql&quot;&gt;CREATE TABLE `visits` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `visitor` int(10) unsigned NOT NULL,
  `visited` int(10) unsigned NOT NULL,
  `when` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `seen` tinyint(3) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `visits_ibfk_2` (`visited`),
  KEY `visits_ibfk_1` (`visitor`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;can i use your method to insert only one visit per day and if is new to have seen = 0 ?
i tried a lot of combinations (using your example) but no luck&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://thewebfellas.com/">
    <author>
      <name>rakesh</name>
    </author>
    <id>tag:thewebfellas.com,2010-01-18:12729:30065</id>
    <published>2011-09-27T09:20:17Z</published>
    <updated>2011-09-27T09:20:17Z</updated>
    <category term="Blog"/>
    <link href="http://thewebfellas.com/blog/2010/1/18/conditional-duplicate-key-updates-with-mysql" rel="alternate" type="text/html"/>
    <title>Comment on 'Conditional duplicate key updates with MySQL' by rakesh</title>
<content type="html">&lt;pre&gt;&lt;code class=&quot;sql&quot;&gt;insert into maintenance.sap_promotions_item_level 
select * from pos.promotions_item_level  ON DUPLICATE KEY  UPDATE 
maintenance.sap_promotions_item_level.SCALING_ID=pos.promotions_item_level.SCALING_ID,
maintenance.sap_promotions_item_level.DISC_INDICATOR=pos.promotions_item_level.DISC_INDICATOR,
maintenance.sap_promotions_item_level.QTY_QUALIFIER=pos.promotions_item_level.QTY_QUALIFIER,
maintenance.sap_promotions_item_level.DISCOUNT_PERCENT=pos.promotions_item_level.DISCOUNT_PERCENT,
maintenance.sap_promotions_item_level.PRICE_OFF_VALUE=pos.promotions_item_level.PRICE_OFF_VALUE,
maintenance.sap_promotions_item_level.PROMO_PRICE_VALUE=pos.promotions_item_level.PROMO_PRICE_VALUE,
maintenance.sap_promotions_item_level.FGOOD1_CODE=pos.promotions_item_level.FGOOD1_CODE,
maintenance.sap_promotions_item_level.FGOOD1_QTY=pos.promotions_item_level.FGOOD1_QTY,
maintenance.sap_promotions_item_level.FGOOD2_CODE=pos.promotions_item_level.FGOOD2_CODE,
maintenance.sap_promotions_item_level.FGOOD2_QTY=pos.promotions_item_level.FGOOD2_QTY,
maintenance.sap_promotions_item_level.FGOOD3_CODE=pos.promotions_item_level.FGOOD3_CODE,
maintenance.sap_promotions_item_level.FGOOD3_QTY=pos.promotions_item_level.FGOOD3_QTY,
maintenance.sap_promotions_item_level.FGOOD4_CODE=pos.promotions_item_level.FGOOD4_CODE,
maintenance.sap_promotions_item_level.FGOOD4_QTY=pos.promotions_item_level.FGOOD4_QTY,
maintenance.sap_promotions_item_level.VALID_FROM_DATE=pos.promotions_item_level.VALID_FROM_DATE,
maintenance.sap_promotions_item_level.VALID_TO_DATE=pos.promotions_item_level.VALID_TO_DATE,
maintenance.sap_promotions_item_level.modified_on=pos.promotions_item_level.modified_on;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I have to add the if or where condition to add the date constraint 
ie: &lt;code&gt;if (pos.promotions_item_level.modified_on &gt; select max(modified_on) from maintenance.sap_promotions_item_level)&lt;/code&gt; then only update or insert should happen.&lt;/p&gt;
&lt;p&gt;thanks&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://thewebfellas.com/">
    <author>
      <name>Philippe</name>
    </author>
    <id>tag:thewebfellas.com,2010-01-18:12729:21874</id>
    <published>2010-12-15T20:33:29Z</published>
    <updated>2010-12-15T20:33:29Z</updated>
    <category term="Blog"/>
    <link href="http://thewebfellas.com/blog/2010/1/18/conditional-duplicate-key-updates-with-mysql" rel="alternate" type="text/html"/>
    <title>Comment on 'Conditional duplicate key updates with MySQL' by Philippe</title>
<content type="html">&lt;p&gt;Just saved me a couple hours of pulling my hair out... thanks !&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://thewebfellas.com/">
    <author>
      <name>Rob Anderton</name>
    </author>
    <id>tag:thewebfellas.com,2010-01-18:12729:20725</id>
    <published>2010-11-13T23:13:35Z</published>
    <updated>2010-11-13T23:13:35Z</updated>
    <category term="Blog"/>
    <link href="http://thewebfellas.com/blog/2010/1/18/conditional-duplicate-key-updates-with-mysql" rel="alternate" type="text/html"/>
    <title>Comment on 'Conditional duplicate key updates with MySQL' by Rob Anderton</title>
<content type="html">&lt;p&gt;Have you tried something like this?&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;sql&quot;&gt;INSERT INTO visits(site, lastvisit)
VALUES (1, 2010-01-19 10:23:11')
ON DUPLICATE KEY UPDATE
lastvisit = IF(DATE_ADD(lastvisit, interval 7 minute) &amp;lt; NOW(), VALUES(lastvisit), lastvisit);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If that's no help, post the full query you're trying to perform and I'll take another look.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://thewebfellas.com/">
    <author>
      <name>paul</name>
    </author>
    <id>tag:thewebfellas.com,2010-01-18:12729:20695</id>
    <published>2010-11-13T00:26:11Z</published>
    <updated>2010-11-13T00:26:11Z</updated>
    <category term="Blog"/>
    <link href="http://thewebfellas.com/blog/2010/1/18/conditional-duplicate-key-updates-with-mysql" rel="alternate" type="text/html"/>
    <title>Comment on 'Conditional duplicate key updates with MySQL' by paul</title>
<content type="html">&lt;p&gt;i just want the following condition in the IF part if it is true&lt;/p&gt;

&lt;p&gt;if
DATE_ADD(&lt;code&gt;lastvisit&lt;/code&gt;, interval 7 minute) &amp;lt; NOW()
then update&lt;/p&gt;

&lt;p&gt;but I just don't seem to get the right syntax
can anybody help me?&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://thewebfellas.com/">
    <author>
      <name>Jeremy James</name>
    </author>
    <id>tag:thewebfellas.com,2010-01-18:12729:13250</id>
    <published>2010-02-11T10:34:16Z</published>
    <updated>2010-02-11T10:34:16Z</updated>
    <category term="Blog"/>
    <link href="http://thewebfellas.com/blog/2010/1/18/conditional-duplicate-key-updates-with-mysql" rel="alternate" type="text/html"/>
    <title>Comment on 'Conditional duplicate key updates with MySQL' by Jeremy James</title>
<content type="html">&lt;p&gt;great idea to put the if clause as condition in the update part, i was wondering how to solve that problem!thanks for the hint
best regards from germany&lt;/p&gt;</content>  </entry>
</feed>

