<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
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/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
> <channel><title>beer planet &#187; swap</title> <atom:link href="http://beerpla.net/tag/swap/feed/" rel="self" type="application/rss+xml" /><link>http://beerpla.net</link> <description>where things have nothing to do with beer - tutorials, tips, how-tos, thoughts, hacks, and other techy nonsense</description> <lastBuildDate>Thu, 17 May 2012 22:50:53 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.2</generator> <atom:link rel='hub' href='http://beerpla.net/?pushpress=hub'/> <item><title>Swapping Column Values in MySQL</title><link>http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/</link> <comments>http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/#comments</comments> <pubDate>Wed, 18 Feb 2009 00:53:47 +0000</pubDate> <dc:creator>Artem Russakovskii</dc:creator> <category><![CDATA[Databases]]></category> <category><![CDATA[Linux]]></category> <category><![CDATA[MySQL]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[column]]></category> <category><![CDATA[database]]></category> <category><![CDATA[swap]]></category> <category><![CDATA[value]]></category> <guid
isPermaLink="false">http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/</guid> <description><![CDATA[<p>Today I had to swap 2 columns in one of my MySQL tables. The task, which seems easily accomplishable by a temp variable, proved to be a bit harder to complete. But only just a bit.</p><p>Here are my findings:</p><ol><li><p>The</p><div
class="wp_syntax"><div
class="code"><pre>UPDATE swap_test SET x=y, y=x;</pre></div></div><p>approach doesn&#039;t work, as it&#039;ll just set both values to y.</p><p><div
class="note"><div
class="notetip">PostgreSQL seems to handle this query differently, as it apparently uses the old values throughout the whole query. [<a
href="http://www.postgresql.org/docs/8.3/static/sql-update.html">Reference</a>]</div></div></p></li><li><p>Here&#039;s a method that uses a temporary variable. Thanks to Antony from the comments for the &#34;IS NOT NULL&#34; tweak. Without it, the query works unpredictably. See the table schema at the end of the post. This method doesn&#039;t swap the values</p></li>...<div
class=clear></div> <a
href="http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/" class="read_more"><div
class=excerpt-end>Read the rest of this article &#187;</div></a></ol>]]></description> <content:encoded><![CDATA[<p>Today I had to swap 2 columns in one of my MySQL tables. The task, which seems easily accomplishable by a temp variable, proved to be a bit harder to complete. But only just a bit.</p><p>Here are my findings:</p><ol><li><p>The</p><div
class="wp_syntax"><div
class="code"><pre>UPDATE swap_test SET x=y, y=x;</pre></div></div><p>approach doesn&#039;t work, as it&#039;ll just set both values to y.</p><p><div
class="note"><div
class="notetip">PostgreSQL seems to handle this query differently, as it apparently uses the old values throughout the whole query. [<a
href="http://www.postgresql.org/docs/8.3/static/sql-update.html">Reference</a>]</div></div></p></li><li><p>Here&#039;s a method that uses a temporary variable. Thanks to Antony from the comments for the &quot;IS NOT NULL&quot; tweak. Without it, the query works unpredictably. See the table schema at the end of the post. This method doesn&#039;t swap the values if one of them is NULL. Use method #3 that doesn&#039;t have this limitation.</p><div
class="wp_syntax"><div
class="code"><pre>UPDATE swap_test SET x=y, y=@temp WHERE (@temp:=x) IS NOT NULL;</pre></div></div><p><div
class="note"><div
class="notewarning">The parentheses around @temp:=x are critical. Omitting them will cause data corruption.</div></div></p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
</pre></td><td
class="code"><pre>mysql&amp;gt; UPDATE swap_test SET x=y, y=@temp WHERE (@temp:=x) IS NOT NULL;
Query OK, 3 rows affected
Rows matched: 3  Changed: 3  Warnings: 0</pre></td></tr></table></div></li><li><p>This method was offered by Dipin in the comments. I think it’s the most elegant and clean solution. It works with both NULL and non-NULL values.</p><div
class="wp_syntax"><div
class="code"><pre>UPDATE swap_test SET x=(@temp:=x), x = y, y = @temp;</pre></div></div></li><li><p>Another approach I came up with that seems to work:</p><div
class="wp_syntax"><div
class="code"><pre>UPDATE swaptest s1, swaptest s2 SET s1.x=s1.y, s1.y=s2.x WHERE s1.id=s2.id;</pre></div></div><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
</pre></td><td
class="code"><pre>mysql&amp;gt; update swap_test s1, swap_test s2 set s1.x=s1.y, s1.y=s2.x where s1.id=s2.id;
Query OK, 3 rows affected
Rows matched: 3  Changed: 3  Warnings: 0</pre></td></tr></table></div></li></ol><p>Essentially, the 1st table is the one getting updated and the 2nd one is used to pull the old data from.<div
class="note"><div
class="noteclassic">Note that this approach requires a primary key to be present.</div></div></p><p>Test schema used:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td
class="code"><pre>CREATE TABLE `swap_test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `x` varchar(255) DEFAULT NULL,
  `y` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;
&nbsp;
INSERT INTO `swap_test` VALUES ('1', 'a', '10');
INSERT INTO `swap_test` VALUES ('2', NULL, '20');
INSERT INTO `swap_test` VALUES ('3', 'c', NULL);</pre></td></tr></table></div><p>Do you have a better approach? If so, please share in the comments.</p><p>Some references:</p><ul><li><a
title="http://stackoverflow.com/questions/37649/swapping-column-values-in-mysql/" href="http://stackoverflow.com/questions/37649/swapping-column-values-in-mysql/">http://stackoverflow.com/questions/37649/swapping-column-values-in-mysql/</a> – some discussion on various methods, which eventually prompted me to start this post</li><li><a
title="http://www.marcworrell.com/article-3026-en.html" href="http://www.marcworrell.com/article-3026-en.html">http://www.marcworrell.com/article-3026-en.html</a> – discussion on the 2nd approach, which doesn’t work</li></ul><div
class='post_blob_1'>Pass your <a
href="http://www.test-king.com/exams/642-524.htm">642-524</a> exam with highest score using latest <a
href="http://www.test-king.com/exams/642-373.htm">642-373</a> practice questions and <a
href="http://www.test-king.com/exams/642-446.htm">642-446</a> sample exams.</div><div
class="shr-bookmarks shr-bookmarks-expand"><ul
class="socials"><li
class="shr-twitter"> <a
href="http://www.shareaholic.com/api/share/?title=Swapping+Column+Values+in+MySQL&amp;link=http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/&amp;notes=Today%20I%20had%20to%20swap%202%20columns%20in%20one%20of%20my%20MySQL%20tables.%20The%20task%2C%20which%20seems%20easily%20accomplishable%20by%20a%20temp%20variable%2C%20proved%20to%20be%20a%20bit%20harder%20to%20complete.%20But%20only%20just%20a%20bit.%0D%0AHere%20are%20my%20findings%3A%0D%0A%0D%0A%0D%0AThe%0D%0AUPDATE%20swap_test%20SET%20x%3Dy%2C%20y%3Dx%3Bapproach%20doesn%27t%20work%2C%20as%20it%27ll%20just%20set%20both%20values%20to%20&amp;short_link=http://bit.ly/9F5oZC&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=%24%7Btitle%7D+-+%24%7Bshort_link%7D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a></li><li
class="shr-facebook"> <a
href="http://www.shareaholic.com/api/share/?title=Swapping+Column+Values+in+MySQL&amp;link=http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/&amp;notes=Today%20I%20had%20to%20swap%202%20columns%20in%20one%20of%20my%20MySQL%20tables.%20The%20task%2C%20which%20seems%20easily%20accomplishable%20by%20a%20temp%20variable%2C%20proved%20to%20be%20a%20bit%20harder%20to%20complete.%20But%20only%20just%20a%20bit.%0D%0AHere%20are%20my%20findings%3A%0D%0A%0D%0A%0D%0AThe%0D%0AUPDATE%20swap_test%20SET%20x%3Dy%2C%20y%3Dx%3Bapproach%20doesn%27t%20work%2C%20as%20it%27ll%20just%20set%20both%20values%20to%20&amp;short_link=http://bit.ly/9F5oZC&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=5&amp;tags=&amp;ctype=" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a></li><li
class="shr-googlebuzz"> <a
href="http://www.shareaholic.com/api/share/?title=Swapping+Column+Values+in+MySQL&amp;link=http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/&amp;notes=Today%20I%20had%20to%20swap%202%20columns%20in%20one%20of%20my%20MySQL%20tables.%20The%20task%2C%20which%20seems%20easily%20accomplishable%20by%20a%20temp%20variable%2C%20proved%20to%20be%20a%20bit%20harder%20to%20complete.%20But%20only%20just%20a%20bit.%0D%0AHere%20are%20my%20findings%3A%0D%0A%0D%0A%0D%0AThe%0D%0AUPDATE%20swap_test%20SET%20x%3Dy%2C%20y%3Dx%3Bapproach%20doesn%27t%20work%2C%20as%20it%27ll%20just%20set%20both%20values%20to%20&amp;short_link=http://bit.ly/9F5oZC&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=257&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a></li><li
class="shr-reddit"> <a
href="http://www.shareaholic.com/api/share/?title=Swapping+Column+Values+in+MySQL&amp;link=http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/&amp;notes=Today%20I%20had%20to%20swap%202%20columns%20in%20one%20of%20my%20MySQL%20tables.%20The%20task%2C%20which%20seems%20easily%20accomplishable%20by%20a%20temp%20variable%2C%20proved%20to%20be%20a%20bit%20harder%20to%20complete.%20But%20only%20just%20a%20bit.%0D%0AHere%20are%20my%20findings%3A%0D%0A%0D%0A%0D%0AThe%0D%0AUPDATE%20swap_test%20SET%20x%3Dy%2C%20y%3Dx%3Bapproach%20doesn%27t%20work%2C%20as%20it%27ll%20just%20set%20both%20values%20to%20&amp;short_link=http://bit.ly/9F5oZC&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=40&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a></li><li
class="shr-hackernews"> <a
href="http://www.shareaholic.com/api/share/?title=Swapping+Column+Values+in+MySQL&amp;link=http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/&amp;notes=Today%20I%20had%20to%20swap%202%20columns%20in%20one%20of%20my%20MySQL%20tables.%20The%20task%2C%20which%20seems%20easily%20accomplishable%20by%20a%20temp%20variable%2C%20proved%20to%20be%20a%20bit%20harder%20to%20complete.%20But%20only%20just%20a%20bit.%0D%0AHere%20are%20my%20findings%3A%0D%0A%0D%0A%0D%0AThe%0D%0AUPDATE%20swap_test%20SET%20x%3Dy%2C%20y%3Dx%3Bapproach%20doesn%27t%20work%2C%20as%20it%27ll%20just%20set%20both%20values%20to%20&amp;short_link=http://bit.ly/9F5oZC&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=202&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Submit this to Hacker News">Submit this to Hacker News</a></li><li
class="shr-delicious"> <a
href="http://www.shareaholic.com/api/share/?title=Swapping+Column+Values+in+MySQL&amp;link=http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/&amp;notes=Today%20I%20had%20to%20swap%202%20columns%20in%20one%20of%20my%20MySQL%20tables.%20The%20task%2C%20which%20seems%20easily%20accomplishable%20by%20a%20temp%20variable%2C%20proved%20to%20be%20a%20bit%20harder%20to%20complete.%20But%20only%20just%20a%20bit.%0D%0AHere%20are%20my%20findings%3A%0D%0A%0D%0A%0D%0AThe%0D%0AUPDATE%20swap_test%20SET%20x%3Dy%2C%20y%3Dx%3Bapproach%20doesn%27t%20work%2C%20as%20it%27ll%20just%20set%20both%20values%20to%20&amp;short_link=http://bit.ly/9F5oZC&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=2&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a></li><li
class="shr-stumbleupon"> <a
href="http://www.shareaholic.com/api/share/?title=Swapping+Column+Values+in+MySQL&amp;link=http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/&amp;notes=Today%20I%20had%20to%20swap%202%20columns%20in%20one%20of%20my%20MySQL%20tables.%20The%20task%2C%20which%20seems%20easily%20accomplishable%20by%20a%20temp%20variable%2C%20proved%20to%20be%20a%20bit%20harder%20to%20complete.%20But%20only%20just%20a%20bit.%0D%0AHere%20are%20my%20findings%3A%0D%0A%0D%0A%0D%0AThe%0D%0AUPDATE%20swap_test%20SET%20x%3Dy%2C%20y%3Dx%3Bapproach%20doesn%27t%20work%2C%20as%20it%27ll%20just%20set%20both%20values%20to%20&amp;short_link=http://bit.ly/9F5oZC&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a></li><li
class="shr-mail"> <a
href="http://www.shareaholic.com/api/share/?title=Swapping%20Column%20Values%20in%20MySQL&amp;link=http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/&amp;notes=Today%20I%20had%20to%20swap%202%20columns%20in%20one%20of%20my%20MySQL%20tables.%20The%20task%2C%20which%20seems%20easily%20accomplishable%20by%20a%20temp%20variable%2C%20proved%20to%20be%20a%20bit%20harder%20to%20complete.%20But%20only%20just%20a%20bit.%0D%0AHere%20are%20my%20findings%3A%0D%0A%0D%0A%0D%0AThe%0D%0AUPDATE%20swap_test%20SET%20x%3Dy%2C%20y%3Dx%3Bapproach%20doesn%27t%20work%2C%20as%20it%27ll%20just%20set%20both%20values%20to%20&amp;short_link=http://bit.ly/9F5oZC&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=201&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Email this to a friend?">Email this to a friend?</a></li></ul><div
style="clear: both;"></div></div> Similar Posts:<ul><li><a
href="http://beerpla.net/2009/03/18/mysql-indexing-considerations-of-implementing-a-priority-field-in-your-application/" rel="bookmark" title="March 18, 2009">MySQL Indexing Considerations Of Implementing A Priority Field In Your Application</a></li><li><a
href="http://beerpla.net/2008/04/17/mysql-conference-liveblogging-optimizing-mysql-for-high-volume-data-logging-applications-thursday-250pm/" rel="bookmark" title="April 17, 2008">MySQL Conference Liveblogging: Optimizing MySQL For High Volume Data Logging Applications (Thursday 2:50PM)</a></li><li><a
href="http://beerpla.net/2008/04/15/mysql-conference-liveblogging-explain-demystified-tuesday-200p/" rel="bookmark" title="April 15, 2008">MySQL Conference Liveblogging: EXPLAIN Demystified (Tuesday 2:00PM)</a></li><li><a
href="http://beerpla.net/2010/03/21/how-to-diagnose-and-fix-incorrect-post-comment-counts-in-wordpress/" rel="bookmark" title="March 21, 2010">How To Diagnose And Fix Incorrect Post Comment Counts In WordPress</a></li><li><a
href="http://beerpla.net/2009/05/11/mysql-deletingupdating-rows-common-to-2-tables-speed-and-slave-lag-considerations/" rel="bookmark" title="May 11, 2009">[MySQL] Deleting/Updating Rows Common To 2 Tables &#8211; Speed And Slave Lag Considerations</a></li></ul><p><a
class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fbeerpla.net%2F2009%2F02%2F17%2Fswapping-column-values-in-mysql%2F&amp;title=Swapping%20Column%20Values%20in%20MySQL" id="wpa2a_2"><img
src="http://beerpla.net/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded> <wfw:commentRss>http://beerpla.net/2009/02/17/swapping-column-values-in-mysql/feed/</wfw:commentRss> <slash:comments>27</slash:comments> </item> </channel> </rss>
