<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.2.2" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: How to write INSERT IF NOT EXISTS queries in standard  SQL</title>
	<link>http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/</link>
	<description>Stay curious!</description>
	<pubDate>Thu, 24 Jul 2008 00:22:39 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.2</generator>

	<item>
		<title>By: Brent</title>
		<link>http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/#comment-14299</link>
		<author>Brent</author>
		<pubDate>Sat, 15 Mar 2008 23:01:02 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/#comment-14299</guid>
		<description>For MySQL users, this idea might work, although it may be clunky. I'd be happy to hear any means of improving efficiency here!

/*1. Check if record exists. Assign @ifExists the recordID.*/
SELECT @ifExists := ifnull(recordID,0) FROM table where url = 'http://www.disney.com';

/*2. Assign @stmt to a valid SQL statement. If recordID = 0, the record doesn't exist, so use an INSERT statement; otherwise, use a dummy statement. Remember to escape apostrophes within the control clause, as in "\'".*/
SET @stmt = IF(@ifExists = 0,'INSERT INTO target (field1) values(\'field1 value\');','SELECT null;');

/*3. Prepare and Execute @stmt*/
PREPARE stmt FROM @stmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;</description>
		<content:encoded><![CDATA[<p>For MySQL users, this idea might work, although it may be clunky. I&#8217;d be happy to hear any means of improving efficiency here!</p>
<p>/*1. Check if record exists. Assign @ifExists the recordID.*/<br />
SELECT @ifExists := ifnull(recordID,0) FROM table where url = &#8216;http://www.disney.com&#8217;;</p>
<p>/*2. Assign @stmt to a valid SQL statement. If recordID = 0, the record doesn&#8217;t exist, so use an INSERT statement; otherwise, use a dummy statement. Remember to escape apostrophes within the control clause, as in &#8220;\&#8217;&#8221;.*/<br />
SET @stmt = IF(@ifExists = 0,&#8217;INSERT INTO target (field1) values(\&#8217;field1 value\&#8217;);&#8217;,'SELECT null;&#8217;);</p>
<p>/*3. Prepare and Execute @stmt*/<br />
PREPARE stmt FROM @stmt;<br />
EXECUTE stmt;<br />
DEALLOCATE PREPARE stmt;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sonia</title>
		<link>http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/#comment-14255</link>
		<author>Sonia</author>
		<pubDate>Wed, 27 Feb 2008 20:03:01 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/#comment-14255</guid>
		<description>Thanks so much. This really sped up my query in comparison to 'not exists'.</description>
		<content:encoded><![CDATA[<p>Thanks so much. This really sped up my query in comparison to &#8216;not exists&#8217;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: manovich</title>
		<link>http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/#comment-14166</link>
		<author>manovich</author>
		<pubDate>Tue, 15 Jan 2008 23:35:26 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/#comment-14166</guid>
		<description>Two approaches to update database row if exists, insert if not - http://dotnettipoftheday.org/tips/update-insert-row-sql.aspx</description>
		<content:encoded><![CDATA[<p>Two approaches to update database row if exists, insert if not - <a href="http://dotnettipoftheday.org/tips/update-insert-row-sql.aspx" rel="nofollow">http://dotnettipoftheday.org/tips/update-insert-row-sql.aspx</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bruno Tarcha</title>
		<link>http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/#comment-14134</link>
		<author>Bruno Tarcha</author>
		<pubDate>Mon, 07 Jan 2008 19:18:32 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/#comment-14134</guid>
		<description>Awesome solution!!
This saved me so much time that I cannot even estimate.
Thank you very much and congratulations!</description>
		<content:encoded><![CDATA[<p>Awesome solution!!<br />
This saved me so much time that I cannot even estimate.<br />
Thank you very much and congratulations!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bogdan</title>
		<link>http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/#comment-13543</link>
		<author>Bogdan</author>
		<pubDate>Thu, 18 Oct 2007 13:14:00 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/#comment-13543</guid>
		<description>to Chris: IGNORE also ignores several other error conditions (such as charset conversion, invalid values etc). Thus it is advisable to first test the query without IGNORE...

It might be a good alternative to use INSERT ... ON DUPLICATE KEY UPDATE syntax with doing nothing in the UPDATE part, but I didn't test this solution myself.</description>
		<content:encoded><![CDATA[<p>to Chris: IGNORE also ignores several other error conditions (such as charset conversion, invalid values etc). Thus it is advisable to first test the query without IGNORE&#8230;</p>
<p>It might be a good alternative to use INSERT &#8230; ON DUPLICATE KEY UPDATE syntax with doing nothing in the UPDATE part, but I didn&#8217;t test this solution myself.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: arasu</title>
		<link>http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/#comment-13386</link>
		<author>arasu</author>
		<pubDate>Sun, 09 Sep 2007 16:04:34 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/#comment-13386</guid>
		<description>the information given to me was valuable.. thank for the information..</description>
		<content:encoded><![CDATA[<p>the information given to me was valuable.. thank for the information..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Leo Muniz</title>
		<link>http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/#comment-11922</link>
		<author>Leo Muniz</author>
		<pubDate>Thu, 28 Jun 2007 00:37:56 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/#comment-11922</guid>
		<description>Hey guys,
      I have a doubt. How can I select * except the columns that I specify?
      Example:
      table users
      id_name, description, age

      select * except age from users;

      i wanna this because i'll probably need to insert columns at the table and want there are in the select query without having to rewrite it.
      thanks</description>
		<content:encoded><![CDATA[<p>Hey guys,<br />
      I have a doubt. How can I select * except the columns that I specify?<br />
      Example:<br />
      table users<br />
      id_name, description, age</p>
<p>      select * except age from users;</p>
<p>      i wanna this because i&#8217;ll probably need to insert columns at the table and want there are in the select query without having to rewrite it.<br />
      thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Leo Muniz</title>
		<link>http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/#comment-11921</link>
		<author>Leo Muniz</author>
		<pubDate>Wed, 27 Jun 2007 22:33:38 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/#comment-11921</guid>
		<description>Hey Chris,
I would like just to thank you. I've been searching for this solution the hole afternoon and now i have it because of you. Thanks.</description>
		<content:encoded><![CDATA[<p>Hey Chris,<br />
I would like just to thank you. I&#8217;ve been searching for this solution the hole afternoon and now i have it because of you. Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Universalist (Chris)</title>
		<link>http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/#comment-8147</link>
		<author>Universalist (Chris)</author>
		<pubDate>Tue, 22 May 2007 01:37:22 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/#comment-8147</guid>
		<description>Alright, with pleasure.

The query is "insert IGNORE into url(url) values(...)".

Explanation:
Lets assume the scenario mentioned above.
We want to do the following (impossible): "insert if not exists into url(url) values(...)"

Now, `url` is the primary key, thus has to be unique. This makes mySQL throw an error if we try to insert a value that already exists and, more importantly: the new value won't be inserted.

Thats practically already an "insert if not exists". We just have to make mySQL not throw an error if it encounters duplicate unique values, and that's exactly what "ignore" is for (it has only this purpose, to my knowledge).

So we just do "insert IGNORE into url(url) values(...)" and everybody should be happy :D</description>
		<content:encoded><![CDATA[<p>Alright, with pleasure.</p>
<p>The query is &#8220;insert IGNORE into url(url) values(&#8230;)&#8221;.</p>
<p>Explanation:<br />
Lets assume the scenario mentioned above.<br />
We want to do the following (impossible): &#8220;insert if not exists into url(url) values(&#8230;)&#8221;</p>
<p>Now, `url` is the primary key, thus has to be unique. This makes mySQL throw an error if we try to insert a value that already exists and, more importantly: the new value won&#8217;t be inserted.</p>
<p>Thats practically already an &#8220;insert if not exists&#8221;. We just have to make mySQL not throw an error if it encounters duplicate unique values, and that&#8217;s exactly what &#8220;ignore&#8221; is for (it has only this purpose, to my knowledge).</p>
<p>So we just do &#8220;insert IGNORE into url(url) values(&#8230;)&#8221; and everybody should be happy :D</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Win</title>
		<link>http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/#comment-6453</link>
		<author>Win</author>
		<pubDate>Wed, 09 May 2007 21:32:20 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2005/09/25/insert-if-not-exists-queries-in-mysql/#comment-6453</guid>
		<description>&lt;p&gt;Hello Chris I am looking for that exact solution for MySQL but I can't find the query could you post it again “INSERT IF NOT EXISTS”.

Thanks&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>Hello Chris I am looking for that exact solution for MySQL but I can&#8217;t find the query could you post it again “INSERT IF NOT EXISTS”.</p>
<p>Thanks</p>
]]></content:encoded>
	</item>
</channel>
</rss>
