<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:media="http://search.yahoo.com/mrss/"><channel><title><![CDATA[insert - All your code are belong to us]]></title><description><![CDATA[Thoughts, stories and ideas on code and technology in general.<br>Blog title inspired by <a href="https://en.wikipedia.org/wiki/All_your_base_are_belong_to_us" target="_blank">this meme</a>]]></description><link>https://allurcode.com/</link><image><url>https://allurcode.com/favicon.png</url><title>insert - All your code are belong to us</title><link>https://allurcode.com/</link></image><generator>Ghost 4.48</generator><lastBuildDate>Wed, 10 Jun 2026 01:30:49 GMT</lastBuildDate><atom:link href="https://allurcode.com/tag/insert/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title><![CDATA[MySQL conditional insert]]></title><description><![CDATA[<!--kg-card-begin: markdown--><p>Sometimes I need to insert some values into the database but only when a certain condition is met. The best example is a newsletter sign up. If someone has already signed up to your newsletter, don&#x2019;t add his email address again. Of course we can run one query</p>]]></description><link>https://allurcode.com/mysql-conditional-insert/</link><guid isPermaLink="false">5b7b0aa2b52b43084c9ea13a</guid><category><![CDATA[conditional]]></category><category><![CDATA[insert]]></category><category><![CDATA[MySQL]]></category><dc:creator><![CDATA[Wojtek]]></dc:creator><pubDate>Fri, 15 Apr 2011 12:30:40 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><p>Sometimes I need to insert some values into the database but only when a certain condition is met. The best example is a newsletter sign up. If someone has already signed up to your newsletter, don&#x2019;t add his email address again. Of course we can run one query to check if this email address already exists and if not run another query to save it in our database.<br>
If for any reason you need to do the whole operation in one query things get a little more complicated.</p>
<h3 id="twoqueries">Two queries</h3>
<p>Lets say we want to insert some data only when field <code>email</code> does not equal <code>test@example.com</code><br>
First, select data:</p>
<pre><code class="language-sql">SELECT `id` FROM `table` WHERE `email` = &apos;test@example.com&apos;;
</code></pre>
<p>if this email address already exists in our database we can let our users know about it. If no records were returned we can continue to insert new data:</p>
<pre><code class="language-sql">INSERT INTO `table` (`email`) VALUES (&apos;test@example.com&apos;);
</code></pre>
<h3 id="onequery">One query</h3>
<p>In a way we combine those two above queries into one:</p>
<pre><code class="language-sql">INSERT INTO `table` (`email`) SELECT &apos;test@example.com&apos; FROM DUAL WHERE NOT EXISTS (
  SELECT `id` FROM `table` WHERE `email` = &apos;test@example.com&apos; LIMIT 1
);
</code></pre>
<p>If the subquery doesn&#x2019;t return any rows the insert will be carried out. In case of subquery finding this given email address, condition is not met and data doesn&#x2019;t get inserted.</p>
<!--kg-card-end: markdown-->]]></content:encoded></item></channel></rss>